applescript - 使用参数将 AppleScript 编译到应用程序中
问题描述
有一个 AppleScript 方法:
on displayError(theErrorMessage)
display dialog theErrorMessage
return "done"
end displayError
我想编译这个脚本并将参数传递到(而不是用 osascript 运行它!)My_Application.app
就像是
osacompile - o My_Application.app My_Script.applescript "This is error message as parameter"
在这种情况下,我将编译可以运行的应用程序。寻找有关如何使用传递参数准确编译脚本的命令。由于编译需要很多时间 - 我只想做那些。运行My_Application.app后,速度比通过 osascript 快几倍。如果输入参数更改 - 只需重新编译应用程序。
一个不错的选择是从正在运行的应用程序中以某种方式收集返回值,但这是另一个问题
解决方案
要获取 AppleScript应用程序的命令行参数,您可以通过一些AppleScriptObjC使用NSProcessInfo。主要问题是没有一种方便的方法可以将结果返回到命令行,因此您需要执行其他操作,例如写入文件。
进程信息参数包括可执行路径,但可以跳过。以这种方式获取参数也适用于osascript
,尽管它的路径也被添加到参数中。
以下将作为脚本或应用程序工作:
use framework "Foundation"
use scripting additions
on run
set arguments to (current application's NSProcessInfo's processInfo's arguments) as list
if first item of arguments contains "osascript" then set arguments to rest of arguments -- skip osascript path
if (count arguments) is 1 then set end of arguments to "no arguments"
repeat with anItem in rest of arguments -- skip the main executable path
displayError(anItem)
end repeat
# osascript still returns the last result
end run
on displayError(theErrorMessage)
display dialog theErrorMessage
return "done"
end displayError
从终端,您可以使用各种命令:
/path/to/application.app/Contents/MacOS/applet "this is a test" "Another test"
open /path/to/application.app --args "this is a test" "Another test"
osascript /path/to/script.scpt "this is a test" "another test"
要使用脚本的参数来编译AppleScript 应用程序,您可以在源文件中使用占位符文本,然后使用脚本或文本编辑器来替换它。然后可以使用osacompile shell 实用程序将源代码编译到应用程序中。它采用文本或脚本文件,结果基于输出文件的扩展名(-o
选项)。
对于一个完整的例子:
Test.applescript 文件(这将用作模板 - 占位符文本将在编辑的输出文件中替换):
display dialog "This is a test.
It is only a test.
The date is ##DATE##
Some name: ##NAME##
An Identifier: ##ID##
End of test."
应用脚本:
use framework "Foundation"
use scripting additions
global arg1 -- this will be the replacement for the NAME parameter
global arg2 -- this will be the replacement for the ID parameter
on run -- example
try
set arguments to (current application's NSProcessInfo's processInfo's arguments) as list
if first item of arguments contains "osascript" then set arguments to rest of arguments
set arguments to rest of arguments
if (count arguments) < 2 then set arguments to getArguments()
set {arg1, arg2} to arguments
processFile(choose file with prompt "Choose the AppleScript source text file:" of type "com.apple.applescript.text")
on error errmess
display alert "Error" message errmess
end try
end run
to getArguments()
set theName to text returned of (display dialog "Enter 'Name' parameter:" default answer "Jane Scripter")
set theID to text returned of (display dialog "Enter 'Identifier' parameter:" default answer "42")
return {theName, theID}
end getArguments
to processFile(theFile) -- get a list of file items for fixPlaceholders
set outputFile to (((path to desktop) as text) & "edited.applescript")
set datePlaceholder to "##DATE##"
set namePlaceholder to "##NAME##"
set idPlaceholder to "##ID##"
set _date to " -e \"s/" & datePlaceholder & "/`date '+%m-%d-%y'`/g\" "
set _name to " -e \"s/" & namePlaceholder & "/" & arg1 & "/g\" "
set _id to " -e \"s/" & idPlaceholder & "/" & arg2 & "/g\" "
set theFile to theFile as text
set output to (do shell script "cat " & quoted form of ((POSIX path of theFile)) & " | sed " & _date & _name & _id)
(my output:output toFile:outputFile)
do shell script "osacompile -o " & quoted form of POSIX path of (((path to desktop) as text) & "Finished.app") & space & quoted form of POSIX path of outputFile
end processFiles
to output:someThing toFile:someFile
try
set fileRef to (open for access someFile with write permission)
set eof of fileRef to 0 -- clear any existing
write someThing to fileRef -- overwrite
close access fileRef
on error errmess
log errmess
try -- make sure file is closed on any error
close access fileRef
end try
end try
end output:toFile:
从终端,可以使用以下命令运行上述应用程序,其中第一个参数将用于“NAME”参数,第二个参数用于“ID”参数:
open /path/to/application.app --args "First, Last" "Yep, looks like you."
应用程序将请求源文件(上面的“Test.applescript”),然后将编辑的源文件和基于它构建的应用程序输出到您的桌面上。
推荐阅读
- javascript - 无法使用 x5c (x509) 公共证书验证 JWT
- html - 在输入类型范围的步骤和拇指上放置图标
- python - Python 性能:平方根和热循环操作策略
- excel - 根据标题复制Excel中的列
- java - IntelliJ Maven JAR“没有主要清单属性错误”
- azure - 将多个 .NET Core 项目的测试覆盖率从 Azure DevOps 发布到 SonarQube 服务器
- flask - flask-sqlalchemy:数据库断开问题
- android - 服务将在哪个线程中运行?
- html - 当一行中的文本较少时无法对齐文本行
- javascript - 当鼠标进入窗口的顶部时做一些事情