Filename-to-Title AppleScript broken by upgrade to macOS 13

I have thousands of photos all viewed via Photos.app > Smart Albums. The ‘albums’ are set up using words in the Title and pics are sorted within each Smart Folder by Title. There’s far too many Smart Folders to edit these parameters.

For years, I have used an Applescript to batch adopt imported photo Filenames as their Titles.

macOS 13 Ventura has broken my system because Apple has decided that to display the Filename in grey is enough, but this doesn’t work for me.

Running the script now achieves nothing. Typing titles in is too time consuming, and it doesn’t appear to be possible to manually copy the filename (from within Photos) and paste it into the Title field …not that I want to do that either! Large numbers of my earlier photos have Titles that differ from their Filenames.

I can create a Smart Folder of new photos based on Filename, but ‘Sort by Title’, perhaps obviously, doesn’t work with greyed out Filenames. And the existing Smart Folders that are set up to display both older and newer photos are ‘sorting’ older photos by Title, but placing the newer photos at the top, ie: in Date order (…out-of-date order!).

Another frustration is that my Filenames are longer than Photos displays. Photos does not allow me to click on a grey filename and run the cursor along, because the click activates the blank Title field and hides the Filename.

I have registered all of this with Apple as a bug, weeks ago – without a result, so far.

This is the script…

(Code posting backticks added by NG. See the Markdown Guide here.)

on run {input, parameters}
    tell application "Photos"
        activate
        set imageSel to (get selection)
        if imageSel is {} then
            error "Please select an image."
        else
            repeat with im in imageSel
                set title to the name of im
                if not (exists (title)) then
                    set the name of im to the filename of im
                end if
            end repeat
        end if
    end tell
    return input
end run

Is there anything I can do to it, to make it work?

Well, first of all, it’s not clear why you need to do this through Automator. Just in case, I commented out the related lines of code. You can uncomment them if you still insist on the Automator version.

Secondly, the name of the Photos’s media element (you also call it the title) always exists. This is either a missing value constant or some real text name. Therefore, there is no point in checking the existence. You should check if it is equal to missing value instead.
.

-- on run {input, parameters}
tell application "Photos"
	activate
	set imageSel to (get selection)
	if imageSel is {} then error "Please select an image."
	repeat with im in imageSel
		tell (contents of im)
			if name is missing value then set name to filename
		end tell
	end repeat
end tell
-- end run

.

Sometimes the value may be equal to “” (empty string). For example, you edited it as empty string, then forgot this change you made in the past. Then, you should compare the name with empty string as well:
.

-- on run {input, parameters}
tell application "Photos"
	activate
	set imageSel to (get selection)
	if imageSel is {} then error "Please select an image."
	repeat with im in imageSel
		tell (contents of im)
			if name is missing value or name is "" then set name to filename
		end tell
	end repeat
end tell
-- end run

.
If you also want to cover cases where the name consists of multiple spaces, then you have to apply a slightly more complex solution:
.

-- on run {input, parameters}
tell application "Photos"
	activate
	set imageSel to (get selection)
	if imageSel is {} then error "Please select an image."
	repeat with im in imageSel
		tell (contents of im)
			set title to name
			if name is missing value then
				set name to filename
			else
				set temp to do shell script "echo " & quoted form of title & " | sed -e 's/^[[:space:]]*//'"
				if temp is "" then set name to filename
			end if
		end tell
	end repeat
end tell
-- end run

Thanks so much for your time. Unfortunately, there’s a but…

If I save one of your scripts as a script, place it in ~Library/Services and give it a Keyboard Shortcut, it does not appear in Photos>Services menu.

If I save one of your scripts as an application, place it in ~Library/Services and give it a Keyboard Shortcut, it too does not appear in Photos>Services menu, but if I double-click on it and give it permission to work within Photos, it changes the sort order, ie: it places all photos in alphanumeric order. It would appear that ‘Sort by Title’ is reading the Filenames after running your script because, unfortunately, the grey Filenames are still there and haven’t been placed in the Title fields – so they are still uneditable and partially unreadable.

To get it to appear in Photos > Services menu, I had to paste it into an Automator QuickAction (which auto-saves into ~Library>Services). So I can now sort from a keyboard shortcut :slight_smile: but not read or edit :frowning:

This only confirms to me that this is a bug (although Apple might think its a feature!).

Thanks again.

That’s why I said earlier that it’s not clear why you are trying to use Automator. Now I understand: you are trying to create a service for an application that is not yours.

You can create (and bind your services to someone else’s application) only for certain objects listed in the Workflow receives current… menu of Quick Action.

The media items of the Photos application are not listed in this list. Therefore, Automator will never create such a service for you.

NOTE:
Placing some random script in ~Library/Services folder doesn’t automatically make it service! The service should be created using Quick Action of Automator then saved as service. In the Workflow receives current… you should choose what objects it runs on. You need to read some guide for How to create custom services using Automator. Anyway, you can’t create service for media items chosen in Photos.app

Thank you for the advice and your time. My original script was saved as an Automator Workflow and used through several macOS upgrades as a Photos shortcut. It only stopped working with the latest OS upgrade, when Apple decided to display the Filename as if it were the Title, without actually placing the text in the Title field. Apple probably think they have done everyone a favour but it has totally fouled up the way I do things. Without my script/shortcut, I cannot read nor edit my photo names. Very frustrating.

If “The media items of the Photos application are not listed in this list. Therefore, Automator will never create such a service for you.” something has changed, because it worked before.