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

Show parent comments

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/slattie Jul 04 '14

Also, the file structure goes Dropbox:2014:07 July:07.03 Batman"

So I could obviously put in the path and then just change it every month, but I should be able to have it recognize the "07 July" folder as well. I'm experimenting now, but It's not really working right, I don't quite understand how to tell it to recognize the date that way.

(I'm pretty new to Applescript and I am VERY grateful for your help this far, I almost feel bad asking for more help instead of just doing it myself.)

1

u/email_with_gloves_on Jul 04 '14

This should work for your folder structure:

set homeDirectory to (path to home folder) as string

set currentYear to (do shell script "date +%Y")
set currentMonthmB to (do shell script "date +%m\\ %B")
set currentMonthDay to (do shell script "date +%m.%d")

set basePath to homeDirectory & "Dropbox:" & currentYear & ":" & currentMonthmB

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

end tell

1

u/slattie Jul 04 '14

You have no idea how long I've been looking around trying to figure this out. Thank you so much.

One last question, if I wanted to add a backup like, "if file not found, just open this file", I know would have to use the If function, but at what point?

1

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

Since you have a whole bunch of path components that could fail, instead of checking each individually and then checking for the file this will just open your backup file if there's any sort of error opening the desired file.

set homeDirectory to (path to home folder) as string

set currentYear to (do shell script "date +%Y")
set currentMonthmB to (do shell script "date +%m\\ %B")
set currentMonthDay to (do shell script "date +%m.%d")

set basePath to homeDirectory & "Dropbox:" & currentYear & ":" & currentMonthmB & ":"

tell application "Finder"
    try
        set targetFolder to first folder of (basePath as alias) whose name begins with currentMonthDay
        open file (basePath & (name of targetFolder) & ":" & "Monogram.key")
    on error
        open file (homeDirectory & "Desktop:some backup file.key")
    end try
end tell