我创建了一个 Macos 应用程序启动器,它正在启动一个 Apple 脚本作为 exec 文件。 问题是我无法拦截脚本中打开命令的文件,或者不知道或在网络上找到某个地方。启动器本身工作正常。为了演示,我将其设置在我的 github 上:
当我使用 open 命令从终端运行它时,例如:test.txt 只是一个简单的文本文件。
打开-a“CvrAppBash.app”“test.txt”--args“--some-arg-1”“--some-arg-2”
我可以在启动的苹果脚本中获得所需的所有信息,以便进一步使用它。还有参数。 但是如何在脚本中获取这里的文件名“test.txt”呢?
此时我的显示对话框给出:
Macos Hfs 路径为:
'MACOSHSIERRA:应用程序:MacPorts:CvrAppBash.app:'
Posix 路径是:
'/Applications/MacPorts/CvrAppBash.app'
应用程序名称是:
'CvrAppBash'
参数是:
'--some-arg-1' '--some-arg-2'
我还不想使用此处的文件名“test.txt”来进一步处理。
我没有得到太多回应。现在找到了解决问题的方法。在这个解决方案中,我不喜欢的一件事是我现在使用苹果脚本编辑器的导出功能创建 .app 捆绑包。我不知道 MacOS 文件夹中名为 Droplet 的二进制文件中使用的代码。我无法检查苹果添加或未添加哪些间谍软件。大多数情况下,当未给出来源时,它确实包含一些跟踪间谍软件。我花了 4 周多的时间才找到这个问题的解决方案,现在我知道它看起来很简单。 下面是我使用的苹果脚本代码。现在它只会记录有兴趣使用的参数,以便进一步对文件和/或其他命令执行某些操作。
use framework "Foundation"
--use framework "AppKit"
use scripting additions
on run
set text item delimiters to linefeed
set myHomePath to system attribute "HOME"
set myLogFile to myHomePath & "/Library/Logs/aLaunchTest.log" as text
set myFile to open for access (myLogFile) with write permission
set eof myFile to 0
close access myFile
set AppBundle to quoted form of text 1 through -2 of POSIX path of text of (path to current application as text)
set AppName to name of current application as text
set AppArgs to (current application's NSProcessInfo's processInfo's arguments) as list
set AppInfoNumbers to count of AppArgs
set AppRunningBin to quoted form of text item 1 of AppArgs as text
set AppXargs to {}
if AppInfoNumbers is greater than 1 then
set AppInfoCnt to 0
repeat with I in AppArgs
set AppInfoCnt to (AppInfoCnt + 1)
if AppInfoCnt is greater than 1 then
set end of AppXargs to quoted form of text of I as text
end if
end repeat
end if
display dialog "Opening Application whitout file in open command or by clicking on app" giving up after 5
set myFile to open for access (myLogFile) with write permission
write "################# Start Logging of TestLaunchCvr Without file to open ##############################" to myFile starting at eof
write "
The Application Bundle is: " & AppBundle as string to myFile starting at eof
write "
The Application Name is: " & AppName to myFile starting at eof
write "
The Application Run Bin is: " & AppRunningBin to myFile starting at eof
if AppXargs is not equal to {} then
set text item delimiters to " "
write "
The Application Args are: " & AppXargs to myFile starting at eof
set text item delimiters to linefeed
end if
write "
################# End Logging Of TestLaunchCvr Without File to open ################################" to myFile starting at eof
close access myFile
end run
on open filepath
set text item delimiters to linefeed
set myHomePath to system attribute "HOME"
set myLogFile to myHomePath & "/Library/Logs/aLaunchTest.log" as text
set myFile to open for access (myLogFile) with write permission
set eof myFile to 0
close access myFile
-- path whitout trailing backslash since the .app is a folder
set AppBundle to quoted form of text 1 through -2 of POSIX path of text of (path to current application as text)
set AppName to name of current application as text
set filepathq to quoted form of POSIX path of filepath as text
set AppArgs to (current application's NSProcessInfo's processInfo's arguments) as list
set AppInfoNumbers to count of AppArgs
set AppRunningBin to quoted form of text item 1 of AppArgs as text
set AppXargs to {}
if AppInfoNumbers is greater than 1 then
set AppInfoCnt to 0
repeat with I in AppArgs
set AppInfoCnt to (AppInfoCnt + 1)
if AppInfoCnt is greater than 1 then
set end of AppXargs to quoted form of text of I as text
end if
end repeat
end if
display dialog "File Name: " & filepathq giving up after 5
set myFile to open for access (myLogFile) with write permission
write "################# Start Logging of TestLaunchCvr With file To Open##############################" to myFile starting at eof
write "
The Application Bundle is: " & AppBundle as string to myFile starting at eof
write "
The Application Name is: " & AppName to myFile starting at eof
write "
The File to open is: " & filepathq to myFile starting at eof
write "
The Application Run Bin is: " & AppRunningBin to myFile starting at eof
if AppXargs is not equal to {} then
set text item delimiters to " "
write "
The Application Args are: " & AppXargs to myFile starting at eof
set text item delimiters to linefeed
end if
write "
################# End Logging Of TestLaunchCvr With File To Open################################" to myFile starting at eof
close access myFile
--end repeat
end open
我在 git hub 上设置了应用程序集 self :
我的 git hub 上的 TestLaunchCvr.app
但我仍在寻找更好的解决方案,而不使用 Droplet 神秘二进制文件。与此同时,我将重新设计我的 Gedit 启动器,我也可以使用 open -a "Gedit" "a file" --args "-some-1" "test-2" "test3 来启动它。