Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting directory of input file (Applescript)

Tags:

applescript

I'm confused - I've been googling for an hour and have tried probably ten different forms of set posixDirectory to POSIX path of (parent of (path to aFile) as string) but I can't seem to get it right.

I'm getting the full POSIX path (including the filename) by doing set posixFilePath to POSIX path of aFile

Now, how do I get the POSIX path of just the directory?? I'm getting various errors depending on what I do... can't make alias.. can't get parent of alias...

I would think this should work but it's not... set posixDirectory to POSIX path of ((parent of aFile))

like image 664
Max Avatar asked Nov 30 '22 17:11

Max


1 Answers

There are couple of ways to do this if you already have the initial path.

From Posix path format

set thePath to "/Users/USERNAME/Documents/Test/selectedTextColour.css"


set textNumber1 to characters 1 thru -((offset of "/" in (reverse of items of thePath as string)) + 1) of thePath as string

or using shell

set thePath to "/Users/USERNAME/Documents/Test/selectedTextColour.css"


set parentPath to do shell script "dirname " & quoted form of thePath

Result: "/Users/USERNAME/Documents/Test"


From Posix file format

set thePath to "Macintosh HD:Users:USERNAME:Documents:Test:selectedTextColour.css"


set textNumber1 to characters 1 thru -((offset of ":" in (reverse of items of thePath as string)) + 1) of thePath as string

Result: "Macintosh HD:Users:USERNAME:Documents:Test"

like image 121
markhunte Avatar answered Dec 06 '22 07:12

markhunte