Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Hacking Image Titles

Hi all,


I have read through Leonie, Old Toad and Ken's post on sorting photos and titles. I have used several AppleScripts from various posts as well.


At the moment, I have some post-Aperture photos which was batched as


Photos - 1

Photos - 2

Photos - 3

...

Photos - 11

Photos - 12

Photos - 13


Sorting by title would see these sorted by Photos - 1, 10, 100, 101, 102...


I have tried hacking some of the scripts on this site but could not get it to work... Could I get some expert help from this site please?


on run {input, parameters}

tell application "Photos"

activate

set imageSel to (get selection)

if imageSel is {} then

error "Please select an image"

else

set n_digits to 2

if (count of imageSel) is greater than 99 then set n_digits to 3

end if


repeat with im in imageSel

set myFileName to filename of im as string

set tmpFileName to (the reverse of every character of myFileName) as string

set myDash to (length of myFileName) - the (offset of "- " in tmpFileName)

set myDot to (length of myFileName) - the (offset of "." in tmpFileName)

set ntext to characters (myDash of myFileName) thru (myDot of myFileName)

repeat while (the length of ntext < n_digits)

set ntext to "0" & ntext -- this adds leading zeros

set myLength to (length of myFileName) - the (offset of " - " in tmpFileName)

set the name of im to characters 1 thru myLength of myFileName & " - " & ntext as text

end repeat

end repeat

end tell

end run


It usually fails at The action “Run AppleScript” encountered an error: “Can’t get myDash of "Photo - 3.tiff".”


Any ideas or suggestions please?


Thanks,

macOS High Sierra (10.13.2)

Posted on Aug 13, 2018 7:03 AM

Reply
Question marked as Best reply

Posted on Aug 13, 2018 9:17 AM

Hi Charley Brown,

I modified the script slightly.

For me, this version is working - give it a try:


--on run {input, parameters}

tell application "Photos"


activate

set imageSel to (get selection)

if imageSel is {} then

error "Please select an image"

else

set n_digits to the length of ((the length of imageSel) as text)


-- compute the number of digits of the number of images

end if


repeat with im in imageSel

set myFileName to filename of im as string

set myDash to the (offsetof "- " inmyFileName) -- the position of the dash

set myDot to the (offsetof "." inmyFileName) -- the position of the dot


-- isolate the filename extension

set fextension to charactersmyDot thru (length of myFileName) of myFileName as text


-- isolate the part up to the dash

set beforeDash to characters 1 thru myDash of myFileName as text


-- isolate the part up to the dash

set afterDash to characters (myDash + 1) thru (myDot - 1) of myFileName as text


set ntext to (afterDash as number) as text


repeat while (the length of ntext < n_digits)

set ntext to "0" & ntext-- this adds leading zeros

end repeat

set newName to beforeDash & " " & ntext & fextension

set the name of im to newName

end repeat

return newName

end tell

--end run


I commented the "on run" part out, so I could test it directly in the Script Editor. You may want to add this again, if you want to start the script in Automator. The "return newName" at the end of the script is just for testing.

your version had a type conflict in this line because of wrong parentheses:

set ntext to characters (myDash of myFileName) thru (myDot of myFileName)

It should have been

set ntext to characters myDash thru myDot of myFileName

Similar questions

3 replies
Question marked as Best reply

Aug 13, 2018 9:17 AM in response to CharlieBrownPeanut

Hi Charley Brown,

I modified the script slightly.

For me, this version is working - give it a try:


--on run {input, parameters}

tell application "Photos"


activate

set imageSel to (get selection)

if imageSel is {} then

error "Please select an image"

else

set n_digits to the length of ((the length of imageSel) as text)


-- compute the number of digits of the number of images

end if


repeat with im in imageSel

set myFileName to filename of im as string

set myDash to the (offsetof "- " inmyFileName) -- the position of the dash

set myDot to the (offsetof "." inmyFileName) -- the position of the dot


-- isolate the filename extension

set fextension to charactersmyDot thru (length of myFileName) of myFileName as text


-- isolate the part up to the dash

set beforeDash to characters 1 thru myDash of myFileName as text


-- isolate the part up to the dash

set afterDash to characters (myDash + 1) thru (myDot - 1) of myFileName as text


set ntext to (afterDash as number) as text


repeat while (the length of ntext < n_digits)

set ntext to "0" & ntext-- this adds leading zeros

end repeat

set newName to beforeDash & " " & ntext & fextension

set the name of im to newName

end repeat

return newName

end tell

--end run


I commented the "on run" part out, so I could test it directly in the Script Editor. You may want to add this again, if you want to start the script in Automator. The "return newName" at the end of the script is just for testing.

your version had a type conflict in this line because of wrong parentheses:

set ntext to characters (myDash of myFileName) thru (myDot of myFileName)

It should have been

set ntext to characters myDash thru myDot of myFileName

Hacking Image Titles

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.