如何从AppleScript创建可执行程序

11
为了运行我的AppleScript程序,我必须打开它并选择“运行”。我希望程序在我单击它时就能自动运行。我尝试将其编译,但似乎没有任何改变或创建新文件。
4个回答

21

2种方法。

1)我首选的方法是激活脚本菜单。如果您使用的是10.6版本,请打开AppleScript编辑器,然后打开偏好设置,在常规选项卡下点击“在菜单栏中显示脚本菜单”。现在你将在屏幕右上角的菜单栏区域得到一个新图标。你可以从那个菜单运行任何AppleScript。(在10.5中的过程不同,但你可以谷歌查找方向)。要将一个AppleScript放入该菜单中,只需转到文件夹~/Library/Scripts并添加您的AppleScript即可。从脚本菜单中选择您的AppleScript将运行它。

2)从AppleScript编辑器中的文件菜单中选择“另存为...”,并将“文件格式”设置为应用程序。然后它就像任何其他应用程序一样工作......只需双击它来运行它。

注意:如果使用脚本菜单...选择一个脚本将运行它。如果您想打开AppleScript编辑器以编辑脚本,则在选择脚本时按住Option键。


请注意:这是AppleScript编辑器,而不是Automator。 - noɥʇʎԀʎzɐɹƆ

7

当您保存AppleScript时,可以选择将其保存为应用程序,这将使其可执行。


如果脚本文件已经存在,您可能无法获得“另存为”选项(只有“保存”),这种情况下,您可能需要通过选择“复制”来欺骗它。然后,您将会得到文件格式的对话框。 - Miek
2
你也可以选择文件->导出为应用程序。 - nvrtd frst
值得一提的是,一个133字节的脚本被保存为101千字节的应用程序 :[. - Alexey

3
如果你考虑像regulus6633提到的第一种方法,你可以使用Quicksilver、TextExpander、osascript等来启动AppleScripts。如果要创建一个应用程序,只需使用第二种方法。

-1

如果正确保存,就是一个可点击的应用程序:

# Old post but this answer may save someone a lot of time.
# In Applescript, make a new script with the following lines (see below).
# Then, after selecting the ''Stay open after run handler'' option,
# save as File Format ''application'' .
# This is the simplest version of a standalone app.
# It is essentially a directory with a Scripts folder and a compiled Applet in it.
# Right click it's icon in the Finder to open the package for a peek inside.
# Other internal items such as icons, files, and other scripts may be added and interacted with.

on run
    #put something here which happens once at startup.
    say "I am running."
end run

on idle
    #put something here which happens repeatedly.
    say "I am waiting."
    return 2 --repeats every 2 seconds. (0 will default to 30 seconds)
end idle

on quit
    #put something here which happens at the end of the program's use.
    say "I am quitting."
    continue quit
end quit

# To accept dropped item's, embed your handlers between tags before saving as shown below here. Enjoy! 

on open droppedItems
    beep --or some other cool thing.
end open

这位 OP 只是想知道如何将默认保存为 .scpt 文件的 AppleScript 脚本另存为应用程序,以便它不会不断在 Script Editor 中打开并需要点击“运行”。-- 而不是如何构建他已经工作的代码! - user3439894
它并不替换OP的代码,而是为OP想要运行为应用程序的任何代码提供包装。以“#在这里放置某些内容...”开头的部分是要放置OP现有代码的位置。“run”、“Idle”和“Quit”处理程序是苹果将Apple脚本转换为可运行应用程序的标准方式,就像OP的2个要求“运行我的AppleScript程序”和“只需在单击它时运行”。 OP的代码被插入到其中一个处理程序中(或放入自动生成的“Scripts”文件夹中的应用程序包中)。那就是字面上的“如何从AppleScript制作可执行程序”。 - Mr. Science
Applescript 可以运行的三种基本方式是:[1]在脚本编辑器中,[2]在 osascript 应用程序(菜单栏的脚本菜单)中,[3]作为扩展名为".app"的 applet。如果要将 applescripts 保存为单击运行应用程序,则需要一个运行处理程序。如果要保持其运行,它需要一个空闲处理程序。如果您想让它退出,则需要退出处理程序。这三个都包含在我的答案中。由于这可以在后台静默运行,因此我添加了语音来证明它正在工作,否则 OP 将不知道应用程序是否正常运行。现在它是一个真正的应用程序,因此可以将其拖到 Dock 并在那里运行! - Mr. Science

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接