将当前窗口的目录复制到Mac上的剪贴板

时间:2013-07-07 19:17:15

标签: scripting applescript

是否可以在Mac上复制当前活动窗口的完整目录?

e.g。说我在Sublime Text中打开了example.txt,我可以执行一个Apple脚本来获取该文件的完整路径吗? (没有去Finder)。

结果是/Users/Bob/Dropbox/example.txt将出现在我的剪贴板中。

1 个答案:

答案 0 :(得分:1)

你可以assign a shortcut to a script这样:

try
    tell application (path to frontmost application as text)
        set the clipboard to (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 "ruby -rcgi -e 'print CGI.unescape ARGV[0][16..-1]' " & quoted form of result
        set the clipboard to result
    end try
end try

第一种方法不适用于Preview,TextMate 2,Sublime Text或iChm,第二种方法不适用于Acorn。