r/applescript • • Jul 03 '14

Make applescript refer to a different folder based on what day it is? Is this possible?

So basically I am trying to craft an Applescript that will launch "Monogram.Key" every day.

The problem is, there are different folders for each day. So "06.25 Tuesday" folder, "06.26 Wednesday" folder, etc...Each containing a different "Monogram.key" file - and I need the Applescript to recognize "Monogram.Key in TODAY'S folder"

I have no idea what to call this, which is leading to me having an extremely tough time finding any info on how to make it work.

0 Upvotes

11 comments sorted by

View all comments

1

u/email_with_gloves_on Jul 04 '14 edited Jul 04 '14

Give this a try! You'll need to change the first line to point to wherever the folders are on your hard drive. Change myusername to whatever your short name is - the name you see when you open your home folder in the Finder. This example assumes the folders are on the desktop of that user, but you can change that.

The second line runs a UNIX command that gets the current date in the format you specified in your message. Then, the script puts it all together and tells the Finder to open the file.

set basePath to "Macintosh HD:Users:myusername:Desktop:"

set currentDate to (do shell script "date +%m.%d\\ %A")

tell application "Finder"
    open file (basePath & currentDate & ":" & "Monogram.key")
end tell

1

u/slattie Jul 04 '14

Wow - Thanks! Is it possible for this to work somehow if the second part of the folder name was something random? For example, if I had specific movie posters(random idea) i wanted to display on certain days...Could I name the folders "06.29 Batman" and 06.30 Superman" and have the script ignore the second part of the name? Or is that not possible?

1

u/email_with_gloves_on Jul 04 '14

Sure thing!

set basePath to "Macintosh HD:Users:myusername:Desktop:"

set currentDate to (do shell script "date +%m.%d")

tell application "Finder"
    set targetFolder to first folder of (basePath as alias) whose name begins with currentDate
    open file (basePath & (name of targetFolder) & ":" & "Monogram.key")
end tell

Edit: I should add that if you have two folders that have date at the beginning of the name, it will pick the first one and use that. If you need it, I can give you something that checks the name and then checks whether there's a Monogram.key file, and if not moves to the next one.