Applescripting a Reminder as an add- folder action - macos

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

added_items is a list of aliases, and name of (added_items) resulted in an error.
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set label index of this_folder to 2
repeat with f in added_items
set n to name of f
tell application "Reminders" to tell list "Downloads"
make new reminder with properties {name:"D/L Complete", body:n, due date:(current date)}
end tell
end repeat
end tell
end adding folder items to
(Save the script in ~/Library/Workflows/Applications/Folder Actions/ and enable the folder action from Folder Actions Setup.)

Related

How To Monitor Contents Of Folder Without Blocking Finder

As you can see from my code below I am extremely new to this. My code just about works, but my major issue is that it hogs up Finder and sometimes it does not set the Desktop picture, but does most of the time!
The script just monitors a folder, and if an "***.jpg" is added then the Desktop picture set to it.
This is my very first script so I have a lot to learn,
set reset to ""
display notification "Alarm Front Active " & (current date) as string
tell application "Finder"
set path_to_sourceFull to ":photo:FRONT CAM 1:20190929:images" -- from nsa310 network drive
set path_to_source to ":photo:FRONT CAM 1:20190929:images" -- from nsa310 network drive
set directory1 to "/Volumes/photo/FRONT CAM 1/20190929/images" as text -- from nsa310 network drive
set path_to_destinationFull to "Macintosh HD:Users:rekordbox:Documents:temp folder 2"
set path_to_destination to ":Users:rekordbox:Documents:temp folder 2"
set directory2 to "/Users/rekordbox/Documents/temp folder 2" as text
repeat while reset = ""
set allok to ""
set filelist to name of every item in folder path_to_source --of startup disk
set listSizesaved to count of filelist
delay 1
repeat while allok = ""
set filelist to name of every item in folder path_to_source --of startup disk
set listSize to count of filelist
if listSize = listSizesaved then
else
set filelist to name of every item in folder path_to_source --of startup disk
set listSize to count of filelist
set LastAddedFile to item listSize of filelist
set allok to "ALARM"
set listSizesaved to listSize -- (save the updated) count
set activefile to (path_to_source & LastAddedFile)
set selectedpicture to (directory1 & "/" & LastAddedFile)
tell application "System Events" to tell every desktop to set picture to selectedpicture
delay 1
display notification "ALARM FRONT TRIGGERED...." & (current date) as string
delay 1
end if
end repeat
end repeat
end tell
The script you want, I think, is this:
on adding folder items to thisFolder after receiving filelist
set droppedFile to first item of filelist
tell application "System Events"
tell every desktop
set picture to droppedFile
end tell
end tell
end adding folder items to
(I've left out the 'Alarm' bit, since I wasn't sure what the point of it was.)
To use this script, copy it into Script Editor, save it in the folder ~/Library/Scripts/Folder Action Scripts/, then open the applet 'Folder Actions Setup'. Add the folder you want on the left-hand side, and choose the file you just saved on the right. It should look something like this:
...where the checkmark on the left shows that folder actions are enabled for the folder (which I called 'test folder') and the script (which I called 'FADtop.scpt') is attached.
Drop an image in the folder, and it should just work.
As a general rule, don't script the Finder unless you absolutely need to; always use System Events. The Finder is a busy app, and scripting it can gum up the system. And also try to avoid this design pattern:
(* Don't do this! *)
repeat
(* test for something *)
delay x
end
The delay command is not particularly resource-efficient. If you really want to use a polling system to test for some event, it's often better to create a stand-alone app with an on idle handler. That way you let the system wake and sleep the script, with significant performance improvements.
EDIT
Since folder actions don't seem to be working with ftp drops onto remote drives, here's a reasonably efficient folder-polling approach. Save the following script as a stay-open application (choose 'Application' as the file type, and click the 'stay open' checkbox). Then launch the application and leave it running in the background.
property dateOfLastFileChosen : missing value
property targetFolder : "/Volumes/photo/FRONT CAM 1/20190929/images"
property idleTime : 300 -- 300 seconds is five minutes
on run
end run
on idle
tell application "System Events"
if exists folder targetFolder then
if dateOfLastFileChosen is missing value then
set recentFiles to every file of folder targetFolder whose visible is true
else
set recentFiles to every file of folder targetFolder whose modification date > dateOfLastFileChosen and visible is true
end if
set newFile to my mostRecentFileOfList(recentFiles)
if newFile is not missing value then
set dateOfLastFileChosen to modification date of newFile
tell every desktop
set picture to (POSIX path of newFile)
end tell
end if
end if
end tell
return idleTime -- check every 5 minutes (300 seconds)
end idle
on mostRecentFileOfList(fileList)
set maxDateObj to missing value
repeat with thisFile in fileList
if maxDateObj is missing value then
set maxDateObj to contents of thisFile
else if modification date of thisFile is greater than modification date of maxDateObj then
set maxDateObj to thisFile
end if
end repeat
return maxDateObj
end mostRecentFileOfList
Without trying to steal the thunder from #Ted Wrigley, whose solution provided the AppleScript code for the folder action, I felt there were enough comments and items for me to add to post it as another answer to the OP's dilemma.
First I will address the tell every desktop set picture to droppedFile lines of code in the following AppleScript Folder Action. If the user has only one monitor/display attached to the computer, but has created several different "Spaces", the tell every desktop set picture to droppedFile lines of code will only change the Desktop Picture for the Desktop of the current active "Space" only. The other Desktop backgrounds will not be changed. However, if the user has several monitors/displays attached to the computer, the tell every desktop set picture to droppedFile lines of code will change the Desktop Pictures for the Desktops of the current active "Space" for each attached monitor/display. If the latter is not the desired result, then tell every desktop should be changed to tell current desktop.
After testing the AppleScript Folder Action code provided by #Ted Wrigley, I noticed the image file being downloaded from an FTP server, to the test folder where I have the folder action script attached to, looked like this before the image was actually finished transferring. Because the file was kind of there and not there, it did not trigger the Folder Action.
Next, I figured I would add a delay to be beginning of the Folder Action code to allow for the transfer of the image file from the FTP server, to complete. I added a delay of 180 seconds to allow for the transfer to complete and it worked. When the transfer was complete, the file look like this.
Depending on how many files you foresee being transferred at any given time along with factoring in for file sizes... It's possible you may need to significantly increase the Delay time.
on adding folder items to thisFolder after receiving theseFiles
delay 180
set newBackground to first item of theseFiles
tell application "System Events"
set picture of current desktop to newBackground -- Single Display Attached
--set picture of every desktop to newBackground -- Multiple Displays Attached
end tell
end adding folder items to

Move dropped files to folder X, otherwise just open X with Applescript

I would like to create an Applescript that mimics the behavior of an alias to a specific folder X. If files are dragged to it, they should be moved to folder X, but if the Applescript is just double-clicked, folder X should be opened.
I am an Applescript novice, but adapting things I've found on the web, I have constructed working pieces: one that moves dragged files to X and one that opens X. I do not know enough to combine them with "if...else" logic, so that if nothing is dragged, the folder is opened; but if things are dragged, they are moved to X but X is not opened.
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
tell application "Finder"
set folderMyFolder to folder "/Users/myname/myfolder"
move theCurrentDroppedItem to folderMyFolder
end tell
end repeat
end open
tell application "Finder" to open "Macintosh HD:Users:myname:myfolder"
When your script contains an open handler, it will be passed items dropped onto the application, while the run handler is the one called when the application is double-clicked. You don't need to do any comparisons, just place the statements to be performed in the appropriate handler, for example:
property targetFolder : ((path to home folder) as text) & "path:to:myfolder"
on run
tell application "Finder" to open folder targetFolder
end run
on open droppedItems
repeat with anItem in droppedItems
tell application "Finder" to move anItem to folder targetFolder
end repeat
end open
Note that the Finder doesn't know about POSIX paths, so to use them you will need to coerce the paths to something the Finder can handle, or use something that does know POSIX paths, such as System Events.

Applescript: on adding folder items to; renaming files without the loop

I created a droplet which would allow me to rename files from an input box.
on adding folder items to este_folder after receiving este_file
display dialog "what's their name?" default answer ""
set text_returned to text returned of the result & ".jpg"
display dialog text_returned
tell application "Finder"
set the name of file este_file to text_returned
end tell
end adding folder items to
It works fine, but it creates a loop where I have to hit cancel again to stop the script, as it thinks a new file has been added. I would like to just rename it once; and then not have the second dialog box pop up again. I have tried rerouting the file to another folder:
on adding folder items to este_folder after receiving este_file
display dialog "what's their name?" default answer ""
set text_returned to text returned of the result & ".jpg"
display dialog text_returned
tell application "Finder"
set the name of file este_file to text_returned
end tell
repeat with anItem in este_file
tell application "Finder"
set destFolder to "Macintosh HD:Users:maxwellanderson:Desktop:BetterinTexas" as alias
move anItem to folder destFolder
end tell
end repeat
end adding folder items to
But that doesn't work either-it doesn't process the renaming portion of the script. Any suggestions on what I should do to get rid of the second dialog box?
The script is being called twice because renaming the file within the watched folder is—for all intents and purposes—like adding a new file into the folder. Therefore, it's called once when the file is actually added; and called a second time when it's renamed.
Moving the file as you suggested will work, but you have to move the file before you rename it. Therefore, shove the code that moves the file near the top of the script, then the renaming bits at the bottom.
As a side-note, I notice you have a repeat with loop to handle multiple file moves, but only one statement that handles a single file renaming. One of these is not like the other. If this watched folder received multiple files at the same time, it would most likely rename them all to the same name, thus potentially over-writing multiple files. If the watched folder only ever receives one file at a time, anyway, then the repeat with loop is redundant.
This code is modelled on yours and will handle a single file move-and-rename (but not a group of files—or, more accurately, as I stated above, it would rename multiple files to the same name, thus overwriting all but the last one in the list):
on adding folder items to este_folder after receiving este_file
set destFolder to POSIX file "/Users/maxwellanderson/Desktop/BetterinTexas" as alias
set text_returned to text returned of ¬
(display dialog "what's their name?" default answer "") ¬
& ".jpg"
display dialog text_returned
tell application "Finder" to ¬
set the name of ¬
(move file este_file to destFolder) ¬
to text_returned
end adding folder items to
If you need it to handle multiple files, then you can wrap everything from set text_returned to to text_returned in a repeat with loop as you've done in your second code block. This will sequentially bring up dialog boxes—one per file—and move/rename the files accordingly.
If you have any questions, or need clarification, leave a comment and I'll get back to you.

AppleScript or Automator flow to import photos into iphoto and set album name as files folder name

I store photo files in folders on my computer before i add them into iphoto.
I want to add the contents of a selected to folder to iphoto and name the new album as the folder name
I have created an automator flow where I do the following
(Select folder initially to work)
Get selected finder items -- This is grabbing the folder
Set Value of Variable -- this is grabbing the full path name and setting a variable with the name
Get Folder contents -- this gets all the photos contained.
Import Files into iphoto -- This adds the photos into iphoto and creates a new album using the variable name.
The issue i have is the variable name sets the full path of the files,
/Users/Johnny/Photos/Dayout
Is the a script that can take just the name of the initial folder "Dayout"
Thanks in advance to anyone who can help
Cheers
John
activate application "SystemUIServer" -- http://www.openradar.me/9406282
tell application "Finder"
activate
repeat with f in (get selection as alias list)
set n to name of f
tell application "iPhoto"
if not (exists album n) then new album name n
import from f to album n
end tell
end repeat
end tell
There is a bug in 10.7 and 10.8 where Finder ignores new windows when getting the selection property. If you open a new Finder window, select some items, and run tell app "Finder" to selection in AppleScript Editor, the result is items selected in some window behind the frontmost window (or an empty list). One workaround is to move focus to another application and back.

Changing the path to a file in iTunes with AppleScript

I am trying to write an iTunes script that takes the selected tracks, moves the files to a different folder on my hard drive, and then update their location in iTunes.
The overall flow will be something like this:
Get selection
Determine path to selection
Move items to destination
Update referenced path in iTunes
I used the suggestion from this question to get the path of a selection in iTunes, and I will be able to figure out how to move the files to where I want them, however, now I want to tell iTunes that the file path is actually some place else.
Anybody know how to go about this?
I figured it out. I had a different error that was making me think this is harder then it is. Here's how I got it to work:
tell application "iTunes"
set s to selection
repeat with c in s
if video kind of c is TV show then
set location of c to <destination directory>
<code to move file>
end if
end tell
The basic idea is to set the location property of each file track item to its new file path. For example:
tell application "iTunes"
tell its first browser window
set currentTrack to first item of (get its selection)
set location of currentTrack to POSIX file "/Users/nad/Music/movedfile.m4a"
end tell
end tell

Resources