19

I m trying to open a folder in Finder using AppleScript. The following is my code. I want the folder WorkSpace to open in Finder, but it opens the Parent Folder /Volumes/MyMacDrive/Mani and highlights the WorkSpace folder. I want the contents of WorkSpace folder, but all I'm getting is its Parent Folder's Contents. What am i missing here ..?

property the_path : "/Volumes/MyMacDrive/Mani/WorkSpace/"
set the_folder to (POSIX file the_path) as alias
tell application "Finder"
    activate
    if window 1 exists then
        set target of window 1 to the_folder
    else
        reveal the_folder
    end if
end tell
DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
Manikandaraj Srinivasan
  • 3,557
  • 5
  • 35
  • 62
  • 2
    Use the `Finder window` class instead of `window` to not have an error if one of these windows is open (information window, preferences window, clipping window and view options window).--> `if Finder window 1 exists then` . The reveal command : Bring the specified object(s) into **view**, use the `open` command as in **adayzdone**'s answer. – jackjr300 Jun 29 '12 at 14:47

4 Answers4

28

As far as I've searched, there seems to be no way to open the folder rather than just highlighting the folder in AppleScript. So I have used:

do shell script "open /Volumes/MyMacDrive/Mani/WorkSpace/"

It worked fine for me but please update me if i'm wrong.

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
Manikandaraj Srinivasan
  • 3,557
  • 5
  • 35
  • 62
  • 1
    There is no difference between your `do shell script` command and the `open` (not **reveal**) command in the Finder, it works for everyone. But, maybe that's an exception to the rule. – jackjr300 Jul 19 '12 at 13:37
  • 2
    For completeness, I'd like to add that if your path is a parameter, you can use `do shell script "open " & quote & pathParameter & quote` and it handles things like spaces in the path. – Tyler Forsythe Dec 13 '13 at 19:22
21

It's actually simpler than it seems:

tell application "Finder" to open ("/Volumes/MyMacDrive/Mani/WorkSpace/" as POSIX file)

or use colons to give an AppleScript path:

tell application "Finder" to open "MyMacDrive:Mani:WorkSpace"

with that you have an open window

brandonjp
  • 3,018
  • 2
  • 28
  • 27
user1700898
  • 319
  • 2
  • 3
  • Well, I have use the second one and it did work for me (Mac OS X 10.10.3, AppleScript 2.4). Thank you! – RoberRM May 06 '15 at 22:04
  • 1
    I had to use something like this (Mojave): `Macintosh HD:Users:MyUser:Downloads`. Replace "MyUser" with the actual name of your user's folder. – StevieD Jan 12 '19 at 14:49
5

Try:

if front Finder window exists then
    set target of front Finder window to the_folder
else
    open the_folder
end if

Edited to incorporate jackjr300's correction. Finder window is the correct class to use.

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
adayzdone
  • 11,120
  • 2
  • 20
  • 37
0

It worked fine for me and bring the Finder to front.

tell application "Finder"
    activate
    open ("/Users/MYNAME/Desktop/Figma/" as POSIX file)
end tell

I just put it in Shortcuts.

Jules
  • 631
  • 6
  • 14