未findAppleScript本地variables

我有这个已经工作了多年的AppleScript:

-- excerpted portion of larger script tell application "Microsoft Excel" set jfFileName to "My Finances " & yearChoice & ".xls" try set theWorkbook to workbook jfFileName on error display dialog "Sorry, Excel doesn't seem to have the file '" & jfFileName & "' open. Please open it, and run again." buttons {"OK"} default button "OK" error -128 end try activate object worksheet "IncomingValues" of theWorkbook end tell 

我昨晚升级到小牛,并有这个脚本的多个问题。

  1. 最后一行(在第一个代码片段中)现在显示语法错误, Expected end of line, etc. but found class name ,并突出显示文本worksheet 。 我删除单词“对象”,现在编译为«event XCELactv» «class cXLW» "IncomingValues" of theWorkbook
  2. 当我运行这个脚本时,结果是:
 tell application "Microsoft Excel" get «class WKBK» "My Finances 2012.xls" --> current application Result: error "The variable theWorkbook is not defined." number -2753 from "theWorkbook" 

我该如何解决? 小牛队是否有一个AppleScriptvariables范围的变化?

(如果我移动try块之外的问题,问题仍然存在。)

更新 :OP报告说,以下最终解决了他的问题:

  • 首先清理字典列表 ,使用/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.fra‌​mework/Support/lsregister -kill -r -domain local -domain system -domain user

  • 打开一个干净的,未编译的纯文本版本的脚本 (其中没有诸如«event XCELactv»之类的值),并重新编译它。


以下是基于OS X 10.9.1上的Excel v14.3.9:

您的症状听起来像是Microsoft Excel.app脚本字典有问题 ,尤其是考虑到您的陈述,即另一个小牛机器不会出现问题。

从AppleScript语言指南:

当AppleScript在脚本编辑器脚本窗口中无法识别术语时,使用双尖括号[chevrons,即«»]。 遇到未在应用程序中定义的术语或脚本打开或编译时可用的脚本添加字典时,会发生这种情况。

是否有可能安装了Microsoft Excel字典的旧版本? 在我的机器上,在您的代码工作的地方,我在已安装的字典列表中看到以下位置: /Applications/Microsoft Office 2011/Microsoft Excel.app
你也看到了吗?
你看到多个 Excel相关字典列出吗? 确保上面提到的唯一一个。

您也可以尝试重build启动服务数据库来清理字典列表(不知道是否真的有帮助):

 /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user 

如果这没有帮助,请尝试重新安装Microsoft Office(一个痛苦,但值得一试)。

至于你当前的代码和你已经尝试过的:

  • 即使脚本按照书面forms工作,目标工作簿当前未打开时的error handling程序也不会启动 – Excel显然悄然地将工作簿设置为missing value – 这可能会解释"The variable theWorkbook is not defined." 错误。

如果您想确保您不会继续进行,除非您有有效的工作theWorkbook参考,请使用以下内容:

 set theWorkbook to missing value try set theWorkbook to workbook jfFileName end try if theWorkbook is missing value then set errMsg to "Sorry, Excel doesn't seem to have the file '" & jfFileName & "' open. Please open it, and run again." display alert errMsg error errMsg number -128 end if 
  • 基于Microsoft Excel.app字典,您需要activate后才能激活工作簿的object

否则, activate之后的任何内容都会被安静地忽略,整个Excel 应用程序将会激活,无论任何工作簿和工作表在当前处于活动状态; 相反,正确的activate object worksheet ...语句将只激活目标工作表的窗口和应用程序的工作表本身,而不激活应用程序本身。

Expected end of line, etc. but found class name错误消息表明,特定于Excel的activate object someObj语法不被识别 – 再次指向字典问题。

你可以尝试这样的事情:

 set yearChoice to "2012" -- testing set jfFileName to "My Finances " & yearChoice & ".xls" tell application "Microsoft Excel" set openWorkbooks to workbooks repeat with aWorkbook in openWorkbooks if aWorkbook's name = jfFileName then set theWorkbook to (contents of aWorkbook) exit repeat end if end repeat try activate object worksheet "IncomingValues" of theWorkbook -- insert your code here on error display dialog "Sorry, Excel doesn't seem to have the file '" & jfFileName & "' open. Please open it, and run again." buttons {"OK"} default button "OK" cancel button "OK" end try end tell 

在OS X 10.9.2 beta 2上,您的代码在Excel v14.3.9上正常工作

您可以尝试重新安装Office套件。

但是,您的代码有一个错误:如果工作簿未打开,则try-catch块无法正常工作。

你可以这样纠正:

 tell application "Microsoft Excel" set jfFileName to "My Finances " & yearChoice & ".xls" set theWorkbook to workbook jfFileName if not ( workbooks contains theWorkbook ) then display dialog "Sorry, Excel doesn't seem to have the file '" & jfFileName & "' open. Please open it, and run again." buttons {"OK"} default button "OK" error -128 end if activate object worksheet "IncomingValues" of theWorkbook end tell