AppleScript:当前应用程序的路径

AppleScript: Path to Current Application

快速提问。我正在尝试使用 AppleScript(Cocoa AppleScript 应用程序)获取当前应用程序的路径。基本上,我试图确定应用程序的位置,以便我可以使用该应用程序操作磁盘映像中文件夹内的文件。我想知道是否有更直接的返回路径的方法,我目前有:

set Var1 to (path to current application as text) & "Resources:File.xml"

哪个returns:"Macintosh HD:Applications:Utilities:Script Editor.app:Resources:File.xml"

很好,只是我想在路径中省略应用程序的名称。我知道我可以通过一些文本操作来做到这一点,但我必须假设除了将它分成几部分之外还有一种更简洁的方法。谢谢!

简而言之,不可能:(很抱歉打破你的泡沫。如果你查看应用程序的字典,你会发现它继承自应用程序。

启动 Apple Script 编辑器并查看:

File > Open Dictionary > Whatever Your App Is Called

因此您将不得不处理基本操作。

在脚本编辑器中试试这个

tell application "Finder"
set current_path0 to container of (path to me) as alias
end tell
set current_path1 to quoted form of (POSIX path of current_path0)

这应该 return 类似于

"'/Users/YourUsername/Library/Autosave Information/'"

作为变量存储的是

'/Users/YourUsername/Library/Autosave Information/'

如果你想变得更漂亮,试试这个

tell application "Finder"
set current_path0 to container of (path to me) as alias
end tell
set current_path1 to quoted form of (POSIX path of current_path0)
tell application "System Events" 
display alert current_path1
end tell

这会产生一个警告框,其中的信息作为变量格式,看起来像这样

'/Users/YourUsername/Library/Autosave Information/'

要获取应用程序的目录,此脚本用于将其另存为应用程序并将其存储在任何可以找到应用程序根目录的位置然后您只需手动输入应用程序名称我使用另一个脚本来存储调用应用程序资源文件夹中文件的应用程序名称

    tell application "Finder"
set current_path0 to container of (path to me) as alias
end tell
set current_path1 to quoted form of (POSIX path of current_path0)
tell application "System Events" 
display alert current_path1
end tell
set current_path2 to text 1 thru -2 of current_path1
set appName to "Example.app'"
set AppFromRoot to current_path2 & appName

其中 return 与此类似

"'/Users/YourUsername/Library/Autosave Information/Example.app'"

存储为变量的内容如下所示

'/Users/YourUsername/Library/Autosave Information/Example.app'

您必须在该文件的根目录中将变量 AppFromRoot 定义为您要进入的应用程序名称,或者您可以在不同的脚本应用程序中使用此脚本以使其成为一个变量,这取决于您的用于