Copy a number or letter and paste the previous or next value

Hello,

I’m working on 2 scripts that will rename a record track within Pro Tools. I’m having trouble with pasting the previous or next value, one for each script.

Let’s say my track name is currently “Ryan 1002”. I want to select the last 2 digits (02), cut, and paste either 03 or 01.

I want to throw an IF statement in there, if it ends with a letter…

Let’s say my track name is currently “Ryan 1001b”. I want to select the last digit (b), cut, and paste either c or a.

The track name could also be something like “Ryan1”, Ryan 1", “Ryan 01”, “Ryan 001”, or “Ryan1001” and then “Ryan1001b” etc… Unfortunately, there are so many variables, I’m trying to come up with something that would cover the vast majority of those.

I’ll upload the current code in a few.

Sincere thanks!
-Ryan

This is what I had so far…

delay 0.2
using terms from application "System Events"
	tell application "System Events"
		delay 0.1
		keystroke ".26."
-- this recalls the memory location #26 and makes certain tracks show and at certain heights

	end tell
end using terms from

tell application "Extra Suites"
	ES move mouse {160, 345}
-- this is where the track name is on the record track

	delay 0.2
	ES click mouse with double click
-- double clicking is how the track is renamed

	delay 0.1
	ES move mouse {(2304 / 4), (1024 / 4)}
-- this is just a dumb math thing I was playing with. it's the dimensions of the 2 desktops and it moves the cursor to the center of the first screen

end tell
using terms from application "System Events"
	tell application "System Events"
		delay 0.1
		key code 124
-- the current track name shows and is highlighted. the right arrow makes the cursor at the end of the text field

		key code 123 using {shift down}
		key code 123 using {shift down}
-- this selects the last two characters

		delay 0.1
		keystroke "x" using {command down}
-- this cuts the last two characters

		delay 0.1
		set cueNumber to the clipboard
-- this sets what was just cut to "cueNumber"

		delay 0.1
		set nextCue to ((cueNumber) + (1))
-- This is where I'm trying to get the next cue name

		delay 0.1
		keystroke nextCue
-- Here is where I'm trying to have the computer type or paste the new value

	end tell
end using terms from

Now that I think about it, here’s what I would need the IF statement to do.
¢ if cueNumber is completely a number (02) then make the new value 03 or 01.

¢ If cueNumber begins with an alpha character then get the previous/next value of the 2nd of the 2 characters. (if n1, make the new value n2 or n0).

¢ If cueNumber ends with an alpha character then get the previous/next value of the 2nd of the 2 characters. (if 1n, make the new value 1o or 1m).

I realize that if I’m switching from “Ryan 1099” to the next cue, it would create “Ryan 10100” with my method instead of “Ryan 1100”, but the few times this would happen, I could manually rename. I also don’t know what to do if it was “Ryan 1001z” and I want to go to the next one, but this NEVER happens. Unless there’s a better method than my limited scripting knowledge.

-Ryan

“using terms from” is only necessary if you’re writing a script telling one app to use parts of another. You don’t need it for System Events.

Not at all clear where you’re getting those key codes.

The following vanilla AppleScript will get next and previous values for any lower case letter entered:


set alph to "abcdefghijklmnopqrstuvwxyz"

set Ltr to text returned of (display dialog "Enter a lower case letter" default answer "")
-- Note: works with an upper case letter too, but numbers will be interpreted as letters in the string
set Off to (offset of Ltr in alph)
set Nxt to text ((Off + 1) mod 26) of alph -- rolls over to beginning of alph if Ltr is "z"
if Off = 1 then -- alternative to mod formulation, you could just test and force the rollover
	set Prv to "z"
else
	set Prv to text (Off - 1) of alph
end if

display dialog "Given the letter " & Ltr & " then:" & return & return & "the next letter is " & Nxt & return & "the previous letter is " & Prv

Next and Previous numbers don’t need anything so elaborate except for the rollover.

Writing back 12 years later, but this is a really clever way to do this. Thank you!