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

时间:2013-07-12 10:52:30

标签: applescript posix

我尝试做什么:

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

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

问题:

脚本已经99%了,但我遗漏了一些东西。当我尝试使用activeApp变量时,它不起作用,我收到此错误:

Error Number:System Events got an error: Can’t get application {"TextEdit"}.
-1728

这是脚本:

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

如果我与activeApp交换"TextEdit",一切正常。帮助将不胜感激。

也许这里有一些有用的东西:Get process name from application name and vice versa, using Applescript

4 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:1)

你要求......

set activeApp to name of application processes whose frontmost is true

注意“进程”,这是多个意思,您可以在响应中获得多个进程,因此applescript会为您提供一个名称列表。即使只返回一个应用程序,它仍然是列表格式。另请注意您的错误包含{“TextEdit”}。名称周围的括号表示它是一个列表,因此错误显示问题。

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

set activeApp to name of first application process whose frontmost is true

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

set activeApps to name of application processes whose frontmost is true
set activeApp to item 1 of activeApps

最后,您不应该告诉系统事件来告诉应用程序。将那两个代码块分开。以下是我编写代码的方法。

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

我不能保证“获取正面文件的文件”代码会起作用。这取决于应用程序。并非所有应用程序都能理解该请求。这就是我使用try块的原因。在任何情况下,虽然你可以肯定你正在解决正确的应用程序。祝你好运。

答案 2 :(得分:1)

我一直在使用这段代码,似乎适用于所有Cocoa应用程序(不确定X11):

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

tell application front_app
    -- Your code here
end tell

答案 3 :(得分:0)

这些似乎都不适用于作为应用程序保存并放在Dock上的已编译AppleScript。无论何时运行应用程序,IT都是最前面的,而不是显示其前窗的应用程序。我的Applescript运行时,该应用程序变为非活动状态。如何编写运行时无效的Applescript应用程序?

我可能找到了上面列出的问题的解决方案。只需告诉用户重新激活所需的应用程序,并给他们时间。

tell application "Finder"
   activate
   say "Click front window of your application"
   delay 5
   set myapp to get name of first application process whose frontmost is true 
-- etc.
-- your code
end tell
相关问题