AppleScript将应用程序分配给变量

时间:2012-02-15 20:34:32

标签: applescript dry

安装了多个InDesign后,ScriptEditor会尝试熟悉脚本应运行的版本。但是,我正在寻找干扰我的脚本,以便我只需要在整个脚本中更改一次应用程序名称。

有一个类似的问题(Applescript path to application using variable),但这并不适用于所有事情。问题是,为什么这不适用于所有事情?

以下按预期方式工作:

tell application "Adobe InDesign CS5.5"
  log name --"Adobe InDesign CS5.5.app" 
  log full name --"Mactastic:Applications:Adobe InDesign CS5.5:Adobe InDesign CS5.5.app:"
end

一点干涸的行动:

set v to "Adobe InDesign CS5.5"
set a to application v
log name of a --"Adobe InDesign CS5.5" 
log full name of a 
--SYNTAX ERROR: Expected end of line, etc. but found property
--"name" is highlighted in the ScriptEditor

这是另一个按预期工作的例子:

set f to (choose file)
tell application "Adobe InDesign CS5.5"
  open f without showing window
end tell

然而,这无法像以前那样编译:

set f to (choose file)
set v to "Adobe InDesign CS5.5"
set a to application v
tell a
  open f without showing window
end
--SYNTAX ERROR: Expected “given”, “with”, “without”, other parameter name, etc. but found class name.
--"window" is highlighted in the ScriptEditor

我的环境:

  • OSX 10.6.8
  • ScriptEditor 2.3(118)
  • Applescript 2.1.2

编辑:最后的游戏就是我希望将一些InDesign功能抽象到我自己的类中,如下所示:

InDesign.scpt - 一个抽象InDesign功能的类

on new()
    copy me to self
    --do some initializing
    return self
end new

on _version()
    return "Adobe InDesign CS5.5"
end _version

on _application()
    return application _version()
end _application

on _open(path)
    tell _application() to open path without showing window 
end _open

my_script.scpt - 使用上面抽象的InDesign.scpt

set InDesign to (load script file ("my:path:to:scripts:" & "Indesign.scpt"))'s new()
InDesign's _open("my:path:to:indd:file.indd")

很有可能在AppleScript中不能真正实现上述目标,而ObjectiveC也是我应该寻求做这类事情的地方。但是,似乎某些事情确实像“告诉_application()打开路径”,但“告诉_application()打开路径而不显示窗口”不这样做。

1 个答案:

答案 0 :(得分:1)

怎么样:

set theApplication to "Adobe InDesign CS5.5"

using terms from application "Adobe InDesign CS5.5"
    tell application theApplication
        --do your thing here
    end tell
end using terms from

使用术语from用于让脚本编译,否则你的tell应用程序中的任何内容都不会编译特定于indesign的应用程序块。当应用程序(当应用程序是Web服务或远程计算机时)不存在或在编译时无法访问时,使用术语是很常见的。