2

I’d like to create an Apple script that will use a text path in clipboard and open a new Finder window and go to that path

  • Comments are not for extended discussion; this conversation has been moved to chat. – bmike Sep 12 at 22:55
4

If you can use the Terminal, do

open "`pbpaste`"
4

Assuming that the clipboard contains a string of the form /Users/username/path/to/file, this should work:

tell application "Finder" to reveal (get the clipboard as string) as POSIX file

The use of the verb reveal ensures that, if a path to a file is supplied, the Finder will display that file in its containing folder rather than launching an application to open the file. If this isn't desirable, replace reveal with open.

2

Taking it a step further, this is not quite a direct solution but it incorporates the direct solution within a larger set of commands. This following AppleScript code will copy to the clipboard, the POSIX paths of the item or items currently selected in Finder, only if Finder is the front most application. Otherwise it will copy to the clipboard, the highlighted text (which would be an HFS or POSIX style file path) from the current window in the front most application, and reveal the item in Finder.

I saved this AppleScript code as "Copy File Paths From Finder Or Go To Folder.scpt" to my /Users/My Short Name/Library/Scripts/ folder, which makes it available to use from within any application, by selecting it from the Script menu in the menu bar.

I find this solution to be much more convenient than using Finder's built in "Go To Folder" command.

tell application "Finder" to set finderIsFrontmost to frontmost

if finderIsFrontmost then
    -- Copy Selected Files In Finder, As Path Names
    tell application "System Events" to keystroke "c" using {option down, command down}
else
    -- Copy Selected File Path Text In Frontmost App (HFS or POSIX Style Paths)
    tell application "System Events" to keystroke "c" using {command down}
    delay 0.1
    -- The "-R" Reveals Copied File Path In Finder, Rather Than Opening If It's A Path To A File
    try
        do shell script "open -R " & quoted form of (the clipboard)
    on error
        try
            do shell script "open -R " & quoted form of POSIX path of (the clipboard)
        on error errMsg number errNum
            display alert errMsg message ¬
                "Either The File Or Folder No Longer Exists" & linefeed & linefeed & "OR" & linefeed & linefeed & ¬
                "The Selected Text Contains Starting Or Trailing White Spaces" & linefeed & linefeed & ¬
                "Please Make Sure Your Selected Text Has No Empty Spaces At The Beginning Or End Of Your Selection" as critical buttons {"OK"} giving up after 20
        end try
    end try
end if

enter image description here


enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .