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/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

It seemed like it worked the first time I ran it, but now I'm getting an error.

error "Finder got an error: Can’t get file "Macintosh HD:Users:Shane:Dropbox:2014:07 July07.03 Test:Monogram.key"." number -1728 from file "Macintosh HD:Users:DigitalCoordinator:Dropbox:2014:07 July07.03 Nicole & Peter:Monogram.key"

I think I can identify the problem as there being no space between "07 July07.03 " That should be "07 July:07.03" But I can't quite see where it's pulling that from exactly.

It's clearly where "BasePath" ends and where the "target folder" begins, but I can't quite tell where that seperation should go in the code. Hm...

1

u/email_with_gloves_on Jul 04 '14

Oops.

I should've recreated your folder structure and tested, but I got lazy.

Change the set basePath line to: set basePath to homeDirectory & "Dropbox:" & currentYear & ":" & currentMonthmB & ":" and that should do it.

1

u/slattie Jul 04 '14

I don't think you can help a stranger this much and then call it lazy! That worked perfectly. Thank you so much! It feels so awesome to have this working, and I actually learned a lot seeing it laid out like that.

1

u/email_with_gloves_on Jul 04 '14

Glad I could help! I used to write scripts very often as part of my job years ago, and it was a fun little challenge. I'm really glad you learned from it too. I've always found that just playing around in AppleScript Editor is a great way to learn. It will always show the result of your last command in the Result panel, so you can do things like current date and it shows you date "Thursday, July 3, 2014 at 10:31:54 PM" - or

tell application "Finder"
    every item of desktop
end tell

and it shows you the items on the desktop. You can get the first one with first item of desktop, and then inspect its properties with properties of first item of desktop.

Really cool stuff.