Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用参数将AppleScript编译到应用程序中_Applescript_Osascript - Fatal编程技术网

使用参数将AppleScript编译到应用程序中

使用参数将AppleScript编译到应用程序中,applescript,osascript,Applescript,Osascript,有一种AppleScript方法: on displayError(theErrorMessage) display dialog theErrorMessage return "done" end displayError 我想编译这个脚本,并将参数传递到(而不是用osascript运行它!)我的应用程序.app 差不多 osacompile - o My_Application.app My_Script.applescript "This is error message

有一种AppleScript方法:

on displayError(theErrorMessage)
    display dialog theErrorMessage
    return "done"
end displayError
我想编译这个脚本,并将参数传递到(而不是用osascript运行它!)我的应用程序.app

差不多

osacompile - o My_Application.app My_Script.applescript "This is error message as parameter" 
在这种情况下,我将编译的应用程序,我可以运行。正在查找有关如何使用传递的参数准确编译脚本的命令。因为编译需要很多时间——我只想做一次。运行My_Application.app后,运行速度比通过osascript快几倍。如果输入参数发生更改-只需重新编译应用程序


一个好的选择是以某种方式从运行的应用程序收集返回值,但这是另一个问题

要获取AppleScript应用程序的命令行参数,可以通过一些AppleScriptObjC使用。主要问题是,没有一种简便的方法将结果返回到命令行,因此您需要执行其他操作,例如写入文件

process info参数包括可执行路径,但可以跳过该路径。以这种方式获取参数也适用于
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应用程序,可以在源文件中使用占位符文本,然后使用脚本或文本编辑器替换它。然后可以使用shell实用程序将源代码编译成应用程序。它接受一个文本或脚本文件,其结果基于输出文件的扩展名(
-o
选项)

有关完整的示例:

Test.applescript文件(该文件将用作模板-占位符文本将在编辑的输出文件中替换):

应用程序脚本:

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:

应用程序将要求提供源文件(“上面的Test.applescript”),然后将编辑后的源文件和由此生成的应用程序输出到桌面上。

这与您之前的问题有什么不同?不同之处在于将脚本编译到应用程序,而不是运行-只是编译如果您希望使用某些参数编译脚本,您可以使用正则脚本(或编辑器)用参数替换一些占位符文本。如果要运行已编译的脚本应用程序,可以使用
NSProcessInfo
获取参数。请解释如何编译它?我没有面临成功您可以使用
osacomile
shell实用程序,例如:
osacomile-o/path/to/output.app/path/to/input.applescript
亲爱的,这正是我要找的-您的示例太棒了-谢谢!)亲爱的,还有一个问题-应用程序正在另一个线程中运行。换句话说,我在终端中运行,命令完成(终端已准备好接受新行/代码),但应用程序仍在运行。目标是等待应用程序何时完成,并且仅在允许运行下一个命令之后。有什么想法吗?我不知道你的工作流程在做什么-你是从什么运行它的?到JavaScript的转换是
ObjC.import(“Foundation”);$。NSProcessInfo.processInfo.arguments
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:
open /path/to/application.app --args "First, Last" "Yep, looks like you."