AppleScript 将应用程序分配给变量

发布于 2025-01-06 03:47:23 字数 2132 浏览 2 评论 0原文

安装多个 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() 打开路径而不显示窗口”则不起作用。

With multiple of InDesign installed, ScriptEditor tries to be smart about which version the script should run. However, I'm looking to DRY up my script so that I only have to change the application name once throughout my script.

There is a similar question ( Applescript path to application using variable ) but this doesn't work for everything. The question is, why doesn't this work for everything?

The following works as expected:

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

A little DRYing action:

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

Here is another example that works as expected:

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

However, this fails to compile like before:

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

My Environment:

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

EDIT: The end game in this is that I was hoping to abstract some of the InDesign functionality into my own classes, like so:

InDesign.scpt - a class that abstracts InDesign functionality

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 - uses the abstracted InDesign.scpt above

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

It's a good chance that the above isn't really possible in AppleScript and ObjectiveC is where I should be looking to do this type of thing. However, it seems that certain things do work like "tell _application() to open path" but "tell _application() to open path without showing window" does not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜访吸血鬼 2025-01-13 03:47:23

怎么样:

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 用于让脚本编译,否则您的应用程序中的应用程序块将不会编译特定于设计的内容。当应用程序(当应用程序是 Web 服务或远程计算机时)不存在或在编译时不可访问时,使用术语非常常见。

How about:

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

The using terms from is used to let a script compile otherwise nothing in your tell application theApplication block will compile that is indesign specific. Using terms is quite common when an application (when the application is a webservice or remote machine) doesn't exists or isn't accessible at compile time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文