Get folder containing automator script

I have an Automator script that calls a shell script (bash) to do its work. How can my shell script learn the folder pathname that contains the Automator script/app that invokes it?


In particular, I want the Automator script to create a folder in the same directory as the script, with the new folder's name being today's date in YYYY-MM-DD format. Doing this through a shell script is not a requirement, but I'm a Unix programmer, and don't have AppleScript skills.

MacBook Pro 17", macOS 10.13

Posted on Apr 22, 2019 7:53 AM

Reply
Question marked as Top-ranking reply

Posted on Apr 24, 2019 7:15 AM

The path to the bash script would be $0, but what I was looking for is how the bash script could learn the location of the Automator script, which would be in a different location. There would be one bash script in a central location and possibly multiple Automator scripts placed in the folders where I want date-named folders to be created. HOWEVER, further research resulted in a pure AppleScript solution, which allows double-clicking on an AppleScript saved-as app to create the desired folders.


The actual AppleScript follows. I welcome any suggestions to improve on this novice effort (thank-you, Google!), which contains items that I don't quite understand.


tell application "Finder"

set {year:y, month:mon, day:d} to (current date)

set ys to y as string

set ms to mon * 1 as string

if the length of ms is 1 then set ms to "0" & ms

set ds to d as string

set fname to ys & "-" & ms & "-" & ds

set dname to container of (path to me) as alias

make new folder at folder dname with properties {name:fname}

end tell

4 replies
Question marked as Top-ranking reply

Apr 24, 2019 7:15 AM in response to VikingOSX

The path to the bash script would be $0, but what I was looking for is how the bash script could learn the location of the Automator script, which would be in a different location. There would be one bash script in a central location and possibly multiple Automator scripts placed in the folders where I want date-named folders to be created. HOWEVER, further research resulted in a pure AppleScript solution, which allows double-clicking on an AppleScript saved-as app to create the desired folders.


The actual AppleScript follows. I welcome any suggestions to improve on this novice effort (thank-you, Google!), which contains items that I don't quite understand.


tell application "Finder"

set {year:y, month:mon, day:d} to (current date)

set ys to y as string

set ms to mon * 1 as string

if the length of ms is 1 then set ms to "0" & ms

set ds to d as string

set fname to ys & "-" & ms & "-" & ds

set dname to container of (path to me) as alias

make new folder at folder dname with properties {name:fname}

end tell

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Get folder containing automator script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.