使用参数将AppleScript编译成应用程序 [英] Compile AppleScript into application with parameters
问题描述
有一个AppleScript方法:
There is an AppleScript method:
on displayError(theErrorMessage)
display dialog theErrorMessage
return "done"
end displayError
我想编译该脚本,并将参数传递到其中(而不是使用osascript来运行!) My_Application.app
I wanna compile this script with passing parameter into (not run it with osascript!)My_Application.app
类似
osacompile - o My_Application.app My_Script.applescript "This is error message as parameter"
在这种情况下,我将编译可运行的应用程序.寻找有关如何精确地编译带有传递参数的脚本的命令.由于编译需要很多时间-我只想做一次.在运行 My_Application.app 之后,时间比通过osascript快.如果输入参数已更改-只需重新编译应用程序即可.
In this case, I will have compiled app which I can run. Looking for a command on how exactly to compile the script with passing parameters. As compilation takes a lot of time - I wanna do it only ones. After run My_Application.app what is in times faster than do it via osascript. If input parameters changed - just recompile the application.
一个不错的选择是从运行的应用程序中以某种方式收集返回值,但这是另一个问题
A good option is to collect somehow return value from running app, but it is a little bit another question
推荐答案
To get command line arguments for an AppleScript application, you can use NSProcessInfo via some AppleScriptObjC. The main issue is that there isn't a handy way to return a result to the command line, so you will need to do something else such as write to a file.
进程信息参数包含可执行路径,但是可以跳过.尽管osascript
的路径也已添加到参数中,但以这种方式获取参数也可以使用osascript
.
The process info arguments include the executable path, but that can be skipped. Getting the arguments this way also works with osascript
, although its path is also added to the arguments.
以下内容将用作脚本或应用程序:
The following will work as a script or an application:
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
在终端中,您可以使用各种命令:
From the Terminal, you can use a variety of commands:
/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
选项).
To use arguments to a script for compiling an AppleScript application, you can use placeholder text in a source file, then use a script or text editor to replace it. The osacompile shell utility can then be used to compile the source into an application. It takes a text or script file, with the result based on the extension of the output file (the -o
option).
有关完整示例:
Test.applescript文件(将用作模板-占位符文本将在编辑后的输出文件中替换):
The Test.applescript file (this will be used as a template - the placeholder text will be replaced in an edited output file):
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"参数:>
From the Terminal, the above application can be run by using the following, where the first argument will be used for the "NAME" parameter and the second for the "ID" parameter:
open /path/to/application.app --args "First, Last" "Yep, looks like you."
应用程序将询问源文件(上面的"Test.applescript"),然后将编辑后的源文件和从中构建的应用程序输出到您的桌面上.
The application will ask for the source file ("Test.applescript", above) and then output an edited source file and an application built from it onto your desktop.
这篇关于使用参数将AppleScript编译成应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!