如何使用AppleScript获取最前面的应用程序的名称,并使用它来获取文件路径

pat*_*ick 11 applescript posix

我尝试做什么:

当我在我的一个文本编辑器(TextEdit,Byword,FoldingText)中时,我希望AppleScript显示文件路径.

我想要求最前面的窗口应用程序让我的应用程序名称很好,很容易,然后我可以在下一步请求POSIX路径.

问题:

脚本已经99%了,但我遗漏了一些东西.当我尝试使用activeApp它的变量不起作用,我得到这个错误:

Error Number:System Events got an error: Can’t get application {"TextEdit"}.
-1728
Run Code Online (Sandbox Code Playgroud)

这是脚本:

tell application "System Events"
     set activeApp to name of application processes whose frontmost is true

     --This doesn't work either:
     --do shell script "php -r 'echo urldecode(\"" & activeApp & "\");'"

     tell application activeApp
         set myPath to POSIX path of (get file of front document)
     end tell
     display dialog myPath
end tell
Run Code Online (Sandbox Code Playgroud)

如果我activeApp"TextEdit"一切工作交换.帮助将不胜感激.

也许这里有一些帮助:使用Applescript从应用程序名称获取进程名称,反之亦然

Lri*_*Lri 8

获取path文档的属性或使用系统事件获取value of attribute "AXDocument":

try
    tell application (path to frontmost application as text)
        (path of document 1) as text
    end tell
on error
    try
        tell application "System Events" to tell (process 1 where frontmost is true)
            value of attribute "AXDocument" of window 1
        end tell
        do shell script "x=" & quoted form of result & "
        x=${x/#file:\\/\\/}
        x=${x/#localhost} # 10.8 and earlier
        printf ${x//%/\\\\x}"
    end try
end try
Run Code Online (Sandbox Code Playgroud)

第一种方法不适用于Preview,TextMate 2,Sublime Text或iChm,第二种方法不适用于Acorn.第二种方法需要访问辅助设备才能启用.


reg*_*633 5

你要求...

set activeApp to name of application processes whose frontmost is true
Run Code Online (Sandbox Code Playgroud)

注意“进程”,它是复数,意味着您可以得到多个进程作为响应,因此 applescript 会为您提供一个名称列表。即使只返回一份申请,它仍然是列表格式。另请参阅您的错误包含 {"TextEdit"}。名称周围的括号意味着它是一个列表,因此错误向您显示了问题。

您无法将名称列表传递到下一行代码。因此,您有几个选择。1)你可以只要求1个进程而不是所有进程。这将返回一个字符串而不是列表。试试这个代码...

set activeApp to name of first application process whose frontmost is true
Run Code Online (Sandbox Code Playgroud)

2) 您可以使用“列表的第 1 项”来处理列表。试试这个代码...

set activeApps to name of application processes whose frontmost is true
set activeApp to item 1 of activeApps
Run Code Online (Sandbox Code Playgroud)

最后,您不应该通过系统事件来通知应用程序。将这两个代码块分开。这是我编写您的代码的方式。

tell application "System Events"
    set activeApp to name of first application process whose frontmost is true
end tell

try
    tell application activeApp
        set myPath to POSIX path of (get file of front document)
    end tell

    tell me
        activate
        display dialog myPath
    end tell
on error theError number errorNumber
    tell me
        activate
        display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
    end tell
end try
Run Code Online (Sandbox Code Playgroud)

我不能保证“获取前端文档的文件”代码会起作用。这取决于应用程序。并非所有应用程序都会理解该请求。这就是我使用 try 块的原因。无论如何,您可以确定您正在处理正确的应用程序。祝你好运。


bar*_*els 5

我已经使用这个片段一段时间了,似乎适用于所有 Cocoa 应用程序(不确定 X11):

set front_app to (path to frontmost application as Unicode text)

tell application front_app
    -- Your code here
end tell
Run Code Online (Sandbox Code Playgroud)