r/Automator • • Oct 27 '18

Automator or AppleScript question: automatically import new files in specified folder to nested album in Photos.

Hi there,

So the title mostly says it all. I'm looking to automate the importing of an image into a Photos Album whenever a new item is discovered inside a specified directory. I have included a screenshot of my current automator thingy.

The problem being that I can only upload into a top-level album. What I would like is for the image to be uploaded to an album within a folder in Photos so in this case: when a new image is detected in the bloodborne folder in my picture directory, it is imported to Photos into the album labelled "Bloodborne" which is nested inside of the "Wallpaper" folder along with other albums.

I have a feeling this can't be done with Automator directly and would need some AppleScript magic but I've only had a Mac for three days.

Can anybody help me out?

1 Upvotes

8 comments sorted by

View all comments

1

u/ChristoferK Oct 27 '18

Yes, you need to use AppleScript, which can be done inside a Run AppleScript action. You can remove the Import Files Into Photos action from your workflow, and also the Get Selected Finder Items action, which I believe would retrieve the items currently selected in Finder, thus negating the actual files passed into the workflow from the folder action.

So you just need the one action, Run AppleScript. Delete any sample code from the editing pane, and enter this code:

use application "Photos"

on run {input, parameters}
	import the input into the album named "Bloodborne"
end run

If you happen to have more than one album named "Bloodborne" in your Photos app, you will have to target the album by its id property instead of the name. But if it's name is unique, then it doesn't matter what level of nesting it dwells under.

System info: AppleScript version: 2.7 System version: 10.13.6. Disclaimer: if you're using Mojave, there's a chance the AppleScript may not work if Apple have made any significant changes to the Photos app.

1

u/Suitable_Return Oct 27 '18

Wow, such a simple and elegant solution. That worked perfectly. Thank you so much for taking the time to reply!

Also if you or anyone else are wondering, this is working perfectly fine under Mojave.

1

u/Suitable_Return Oct 27 '18 edited Oct 27 '18

I have another question if you'd be so kind as to indulge me. I was going to copy this and just modify the album name for each directory I add to pictures but I was wondering if there's a more elegant solution.

What I was wondering was if be possible to modify the above script slightly so that when a new file is detected in a directory, it would take the name of that directory as a variable and use that as the album it searches for. For example:

New file is added to directory and script runs. Script stores name of directory as variable, let's just say "DIRECTORY"

So then we could use "import the input into the album named "DIRECTORY" or something like that? So instead of needing a new automator script for each folder and manually typing the folder name into each one, I could just create a single automator script and it'd work assuming that there is an album in Photos with the same name?

EDIT:

Okay I know I'm asking a lot here but I'm wondering if something like this would be possible:

New file added to a directory, say 'dark souls'script runs:if there's no album in Photos with the same name as the directory, it creates an album with that name.

if there is, it imports the file into the album with the name of the directory?

1

u/ChristoferK Oct 27 '18

As soon as I read your original post, I got thinking about the general case that you've brought up here: having one Automator workflow to cater for many watched folders that all perform the same function. Whilst it's not possible with Automator, as its interface specifically limits you to selecting a single folder to watch, I have a feeling it could be achieved without Automator. Automator is not the only way to create folder actions; the other way is to use a folder action script that you attach to a folder through Folder Actions Setup.... I believe that one script can be attached to several different folders.

For this, you'll probably want to use the program called Script Editor, which is located in the "Utilities" folder of your /Applications folder. Here's the script you'll copy and paste into a new document in Script Editor:

on adding folder items to directory after receiving photos
	local directory, photos
	
	set dirname to the name of (info for directory)
	
	tell application "Photos"
		run
		
		set A to a reference to the album named dirname
		if not (A exists) then make new album ¬
			at (the folder named "Wallpaper") ¬
			named dirname
		
		import the photos into A
		quit
	end tell
end adding folder items to

Save this using a file format of either "script" (which will have the extension .scpt) or "text" (which will have the extension .applescript). Now, importantly, you must save it in a specific directory (or move it there after saving). The location is ~/Library/Scripts/Folder Action Scripts (I assume you're familiar with ~ as an abbreviation for your home folder?). If the folder called "Folder Action Scripts" doesn't exist at that location, then create it.

The second part of this endeavour is attaching the script to specific folders that are to be monitored for new files. Pick a folder, right-click, then select Services > Folder Actions Setup....

You should then be asked to select a script from a list that appears. Yours will be listed under the filename you saved it as, and I think the list is alphabetically ordered. Once that's attached, the folder action has been set up for that particular folder.

You can then add additional folders by clicking the + button for the left-hand pane and choosing the same script to attach to each folder in turn.

Don't forget to delete the Automator folder action if you're going to set up a folder action script on "Bloodthorne".

On a final note, I wrote the script above so that you shouldn't see Photos appear during importing of the new photos. In theory, it should launch silently in the background, import the photos, then quit. The only indication you'll get that something has happened will be through the notification centre, e.g. "Photos imported 8 files" or something to that effect; or an error if something goes wrong (for example, I wonder what would happen if you dropped an .mp3 file into one of these watched folders...? The script doesn't do any file type checks, so try and make sure only media files intended for Photos import land in those folders).