Applescripting a Reminder as an add- folder action

Hi, I’m new to applescript but I want to set up a folder action that:

  1. Recognises when a file is added to a folder
  2. Tags said folder red
  3. Adds a Reminder to the “Downloads” reminder list that has the name of the newly-added file as the body text of the reminder
    Using google and Applescript’s record function I’ve frankenscripted this together so far

[code]property dialog_timeout : 30 – set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items

try
	tell application "Finder"
		set FILEname to name of (added_items)
		set label index of folder "untitled folder" of folder "Desktop" of folder "heyjeremyoates" of folder "Users" of startup disk to 2
	end tell
	
	tell application "Reminders"
		set mylist to list "Downloads"
		tell mylist
			make new reminder with properties {name:"D/L Complete", body:FILEname, due date:(current date)}
		end tell
	end tell
end try

end adding folder items to[/code]
Darn thing won’t work. Infuriating. I tested it as a folder action with “test” as the name and body of the reminder and it worked fine. I’m pretty sure I’ve gone wrong somewhere in setting FILEname as the name of the newly copied item because the script as it is now no longer turns the folder red.

The idea behind this is so I can see, from my iPhone/iPad, how many large/scheduled downloads to my home mac (both torrents and large work files - I’ll have a seperate folder action and reminder list for each download folder) there are that are yet to be managed.

It seemed like setting up a Growl/Prowl combo was wasteful if iCloud/Reminders and a dozen lines of code could deliver what I wanted anyway. Ideally I’ll write a second applescript that will delete the reminder when I rename or move the related file, and though I haven’t even thought about how that would work if anyone has any suggestions for it I’d be super grateful

It is a pity you can’t (natively) have OSX notifications pushed to an iOS device linked to the same iCloud account though (with the appropriate granularity)

But I digress - can anyone see what I’m screwing up here?

Thanks in advance for even reading this far

Model: Mac Mini 2010
AppleScript: 2.5.1
Browser: Safari 536.30.1
Operating System: Mac OS X (10.8)

Hi NeverEnoughCoffee and welcome to MacScripter, may I call you just coffee?

added_items is an list of aliases instead of just a single alias object. So you need to wrap a repeat around it to process each alias in case you add multiple files to the folder instead of just 1. At this moment I’m behind a Snow Leopard machine, which doesn’t have reminders, so I can only help you with the Finder part:

on adding folder items to this_folder after receiving added_items
	repeat with thisAlias in added_items
		tell application "Finder" to set label index of thisAlias to 2
	end repeat
end adding folder items to

Thanks for the welcome DJ Bazzie Wazzie, and I’m fine with whatever handle works best.

I’m actually ok with label-changing part of the script (it’s just to change the enclosing folder, and is really there as a check to see where my code was breaking down), but I think you’re probably right in that I’m trying to create a single variable (FILEname) from a list of aliases rather than a single alias object.

Given that each download that triggers the script will be either a single file, or a single folder containing files, do you have any suggestion as to what I should change on this line

set FILEname to name of (added_items)

Would I maybe be better off not using added_items and instead sorting the target directory by date and getting the name from the top file/folder on the list (I’ve previously avoided this because I figure every Finder window will then be date-sorted, and i’m an alphabetised sorta guy). As long as I have some way of getting the name of the item that triggered the folder action and passing it as text to Reminders I’ll be happy as a proverbial

Thanks again for the prompt suggestion, now I know I’ve been thinking about added_items wrong at least

Hi.

If I’ve understood the aims properly, the script could be something like this (not tested in context):

on adding folder items to this_folder after receiving added_items
	tell application "Finder" to set label index of this_folder to 2
	tell application "Reminders" to activate
	
	repeat with thisAlias in added_items
		tell application "Finder"
			if (class of item thisAlias) is folder then
				set theFiles to thisItem's files
			else
				set theFiles to {thisitem}
			end if
		end tell
		
		repeat with thisfile in theFiles
			tell application "Finder" to set FILEname to name of thisfile
			
			tell application "Reminders"
				set mylist to list "Downloads"
				tell mylist
					make new reminder with properties {name:"D/L Complete", body:FILEname, due date:(current date)}
				end tell
			end tell
		end repeat
	end repeat
end adding folder items to

You can use it inside the repeat loop (like your code for reminder) so the complete code would look something like…

on adding folder items to this_folder after receiving added_items
   repeat with thisAlias in added_items
       tell application "Finder"
          set fileName to name of thisAlias
          set label index of thisAlias to 2
        end tell


        tell application "Reminders"
            tell list "Downloads" to make new reminder with properties {name:"D/L Complete", body:fileName, due date:(current date)}
        end tell
   end repeat
end adding folder items to

Again, I don’t have reminders installed so I could be a bit off.

Wow, you guys are seriously humbling - I’ve spent hours tinkering, staring, googling and generally getting nowhere.

Thanks for the advice Mr Garvey, I’ve ended up following Mr Wazzie’s lead on this but that bit you had:

  repeat with thisAlias in added_items
       tell application "Finder"
           if (class of item thisAlias) is folder then
               set theFiles to thisItem's files
           else
               set theFiles to {thisitem}
           end if
       end tell

this is going to come in handy when I get around to scripting the management of these files once downloaded, so thank you (also, kudos to Apple for having a scripting language so easy to read/learn from)

Big thanks DJ Bazzie Wazzie - I was initially worried having a repeat would pull all the files from subfolders and leave me with another list but you were spot on (and without reminders.app to test it on, which is a little awe inspiring to me). I ditched the label index as once I was sure reminders were working I didn’t need it (it was just for debug anyway) so the code ended up being

(*
Script concieved by Jeremy Oates
Script adapted from solution offered by macscripter.net user DJ Bazzie Wazzie. Thanks!
*)

on adding folder items to this_folder after receiving added_items
	
	repeat with thisAlias in added_items
		tell application "Finder"
			set DLname to name of thisAlias
		end tell
		
		tell application "Reminders"
			set mylist to list "Downloads"
			tell mylist
				make new reminder with properties {name:"D/L Complete", body:DLname, due date:(current date)}
			end tell
		end tell
	end repeat
	
end adding folder items to

2 weeks later and the script has been working fine, in the sense that it isn’t crashing or glitching, but it’s not really performing as the mobile download notifier I wanted it to be either. I’ve tinkered with a the line that makes the new reminder so that it’s (hopefully, I can’t test it til I get home) reminding me with an alert/banner instead of just badging the iPhones reminder icon when a download completes, but I want to shift the due date to tomorrow and have no idea how. This is what I’ve done so far

tell mylist
				make new reminder with properties {name:"D/L Complete", body:DLname, remind me date:(current date), due date:(????)}
			end tell

Yeah, not far, I know.
I had a look through the date/time section at http://macscripter.net/viewtopic.php?id=24737 but I’m having a little trouble getting my head around what I need to do in this instance (sorry, pretty new to coding).
Do I break down the current date into variables made of it’s components (D,M,Y), add 2 to D and then create a variable that I can call as a date, or is there a really simple way (a due date:(tomorrow) style plaintext solution)?

Also, does anyone know way to attach a location trigger when creating a reminder? I couldn’t find any reference to it in the reminders.app applescript dictionary, but it’s something you can set in reminders on both iOS and OSX, so I was hoping it could be done despite the lack of documentation.

Hi NeverEnoughCoffee,

You can just add the number of seconds to the date object. Like this:

set current_date to (current date)
set due_date to current_date + 1 * days

1 * days = number of seconds in 1 day

There is nothing in the Reminders dictionary to add location, but maybe there is a way using System Events ui scripting.

gl,
kel

Just thought of a way maybe. Maybe you can create in Reminders a template reminder with the location part. Then I think you can duplicate the reminder. Then set its properties.

Edited: just so you don’t knock yourself out try to optimize the folder action, folder actions have always been not perfect.

gl,
kel

A-ha! When I tried it the first time I had missed a step so my code read

 
--this code will not work, it is just to illustrate my mistake in case someone else makes the same error
tell mylist
make new reminder with properties {name:"D/L Complete", body:DLname, remind me date:(current date), due date:((current date) + 1 * days)}
end tell

Seems obvious what I did wrong now, but that’s what I get for relying on applescript’s “record” function and this-here-forum for all of my applescript knowledge and never having read even a beginners guide to scripting (beyond Logowriter and simple Visual Basic in school). oops :confused: I’m gonna start rectifying that now so I don’t feel like I’m sort of wasting people’s time with basic questions

Thanks kel1, both for the help with the due date and with the advice for location. I can’t test your theory about duplicating a template reminder at the moment, but I’ll post complete code if it does work (and it’s a pretty genius workaround). Ideally I’d like to set up a variation of this script/folder-action on my work iMac which will create a reminder to deal with specified downloads/emails (should one be complete) on my iPhone as soon as I park my car at work so that I know my immediate priorities as soon as I sit down at my desk (just to give a little context).

PS - regarding the imperfection of folder actions, do you know of any circumstances that make folder actions more likely to glitch/not work? I’m fairly new to mac (it’s a work requirement that i’ve quickly come to see as a work perk) and have assumed until recently that the occasional folder action problem I’ve had was due to a fairly outdated mac mini and the number of fullscreen apps/safari tabs I typically had open. Any advice/links would be great :slight_smile:

Hi NeverEnoughCoffee,

Glad you got it working.

I couldn’t get the ‘duplicate’ command to duplicate a reminder. Someone else might know how to do that.

You might be able to import a reminder though, or just add a file to a Reminders folder wherever that is. I’m curious how to do this also.

If you want to see some folder actions quirks, you can search this site for “folder actions”. I think there are a lot of things to do with timings and naming things and etc.

Edited: and btw, you might want to move the ‘current date’ out of the 'tell app “Reminders” block because of keyword conflicts. Here’s an example of how I was trying to duplicate the template reminder:

set cur_date to (current date)
set rem_me_date to cur_date + 1 * hours
set due_date to cur_date + 2 * hours
tell application "Reminders"
	set rem1 to first reminder of list "Templates"
	set rem_list to first list whose name is "Reminders"
	duplicate rem1 to rem_list with properties {name:"Reminder1", due date:due_date, remind me date:rem_me_date}
end tell

gl,
kel

I’ve been working on this all morning and just have an absurd idea of how you can make reminders with locations by script. I hope someone has an easier way. Then if that doesn’t work, you need to use the cursor.

Hi NeverEnoughCoffee,

How many reminders do you have a day for each location?

Hi NeverEnoughCoffee,

Good news maybe. I think I found the answer:

Unfortunately, I might crash out soon.

Later,
kel

Hi,

What I’ve noticed in the ics file is that the only difference between a blank reminder (has just a name) and one with the location turned on is this part:

Interesting that the latitude and longitude is in there. I replaced my own info with labels. n is a digit and start of the address. White space is allowed anywhere in the location.

I hate to write to or swap application files, but sometimes it works.

Wow, that’s some neat detective work. I have no idea how to even start using the info you’ve dug up (i can’t even find where the app keeps the individual reminder files, assuming that’s what the ics files are)
Your question about the number of alerts made me question the whole concept of me using location-based reminders for this work script - I will sometimes have 10+ files/emails waiting, but do I want 10 seperate alert banners, each with accompanying sound, hitting me all at once as I arrive in the office? I think I might want to rethink exactly how I want this script to function. It seems like it’s well beyond my ability, though I’m still genuinely interested to see if there is a solution

A night of being woken up by reminders has me looking to tinker with the revised code for the original time-based reminder script too. Currently (version 3, I’ve not taken your advice about renaming yet, but it’s coming) the script looks like this

(*
Script concieved by Jeremy Oates
Script adapted from solutions offered by macscripter.net users DJ Bazzie Wazzie and kel1. Thanks!
*)

on adding folder items to this_folder after receiving added_items
	-- remind_date delay seems necessary for my iphone to recieve reminder in time. I don't know whether that's an icloud thing, a cellular carrier issue, or what.
	
	set current_date to (current date)
	set remind_date to current_date + 10 * minutes
	set due_date to current_date + 1 * days
	
	repeat with thisAlias in added_items
		tell application "Finder"
			set DLname to name of thisAlias
		end tell
		
		tell application "Reminders"
			set mylist to list "Downloads"
			tell mylist
				make new reminder with properties {name:"D/L Complete", body:DLname, remind me date:remind_date, due date:due_date}
			end tell
		end tell
	end repeat
	
end adding folder items to

The new plan is to insert some lines immediately after “set remind_date…” that checks to see if remind_date is between the hours of 0030 and 0930 (not my actual sleeping hours), and if it is, then shifts the remind_date to 0945. I’m trying to quickly scan the forums about date/time cos I know I read something about that when i was overcomplicating on an earlier problem. I know it’s there somewhere :confused:

Hi NeverEnoughCoffee,

I too have that same problem with a barage of Notifications from Notification Center. Like after I don’t download the email for a couple of weeks. Then I get the mail and … I never was a fan of email. It makes you have to do something.

Will be working on modifying the reminders file tomorrow.

Have a good day,
kel

Hi NeverEnoughCoffee,

I think that if you want to script locations, then you should use Calendar events. It’s much easier and less dangerous.

gl,
kel

Thanks kel1, there certainly seems to be more existing scripts on the forum dealing with calendars so it’s probably the way to go. That said, I do have a crowded enough calendar app already, so I might exploit the regularity of work hours and what i’ve learnt about manipulating the time and date of reminders so as not to rely on location data for work reminders. I might start a new, purely speculative, thread on ways to script location-based reminders -it seems a fair bit beyond me, but i hope you’ll re-contribute your earlier post with what you learned about from the ics file and that it gets the ball rolling toward a usable script (surely plenty could find a use for it)

Also, if you have time I have a quick question about the alteration i’ve made to the code I’ve posted so far (I believe I understand the syntax of the code I’m using, but that doesn’t make it true) specifically the line i’ve marked with **

on adding folder items to this_folder after receiving added_items
	
	set current_date to (current date)
	set due_date to current_date + 1 * days
	
	-- This checks that reminders won't remind before 9:30am, which is 34200 seconds after midnight
	set time_in to time of (current date)
	if time_in < 34200 then
****		set remind_date to "9:30:00 AM"
	else
		set remind_date to current_date + 10 * minutes
	end if

if I just set remind_date to “9:30:00 AM” will it recognise that as being both part of a date-time string and will it default the date portion of the string to the current date? I’m a little worried that setting remind_date without it directly referencing (current date) will mean it’s just a text string (and I can only test the script after midnight as is)

UPDATE: I tested the changes early by making the “<” a “>”; the script seems to work only in situations where it adds 10 minutes to to set remind_date. When it has to set remind_date to 9:30 the reminder app opens on the mac running the script but no reminder is created <<<<<

Which brings me to another question - while I’m quite proud of the simplicity of this just checking the number of seconds since midnight to work out how to set the remind_date (I was worried my original coding effort would delay reminders that arrived between 12-9pm as well so I re-read the forum and re-wrote the script) I also know it’s not terribly adaptable. Should I want to adapt the code to not issue certain reminders IN work hours I’m not sure how to do that. Can I use a simple phrase like

    if time_in > 32400 and time_in < 63000 then

or do I have to write something more complex to make it adaptable.

Thanks again for your help so far
nec

Hi again, I read some posts and thought I had it but I’m still getting the same result - reminders will appear if a download completes after 9:30, but the reminders that should be delayed until 9:30 do not appear at all. I’m going wrong but I can’t work out where

	set current_date to (current date)
	set due_date to current_date + 1 * days
	
	-- This checks that reminders won't remind before 9:30am, which is 34200 seconds after midnight
	set time_in to time of (current date)
	if time_in < 34200 then
		set {year:y, month:m, day:d} to (current date)
		set delayed_date to (m & "/" & d & "/" & y & space & "9:30:00 AM")
		date delayed_date
		set remind_date to result
	else
		set remind_date to current_date + 10 * minutes
	end if

Thanks in advance if anyone has any suggestions