Prepending folder name?

My current script uses the following as part of a larger workflow:

display dialog "ENTER JOB CODE:" default answer "JobCode"
set projCode to text returned of result
set projectFolder to choose folder with prompt "SELECT PROJECT FOLDER for " & projCode

The user inputs projCode in the following format: ABCDEXY123. It’s used to rename files using the same code with suffixes.

Is it possible to get just the “XY123” part of that code to prepend projectFolder’s name as the final step in the overall script without having to add another step to input just that part projCode? Any insights would be appreciated. Thanks!

If all you want to do is get the last 5 characters of projCode, the following line will work if you insert it after the line that sets projCode.


set prePend to characters ((length of projCode) - 4) thru (length of projCode) of projCode as text

The following will also work.


set prePend to characters (-5) thru (-1) of projCode as text

haolesurferdude, neither of those is a good idea. They’re inefficient – you’re making lists and then concatenating them – and they’re error-prone because the result will depend on the value of text item delimiters when you run it. Better to use this:

set prePend to text -5 thru -1 of projCode

Thanks for the tips! However I think I mangled the follow-through. I got a stack overflow error when I tried:

set name of projectFolder to prePend & " - " & projectFolder as string

Try:

set name of projectFolder to (prePend & " - " & (get name of projectFolder))

Thanks for the quick response! Unfortunately I got an error stating that it couldn’t get the name of the alias for the folder (followed by the full path name).

Hi.

You have to tell either System Events or the Finder to get and set the folder’s name:

display dialog "ENTER JOB CODE:" default answer "JobCode"
set projCode to text returned of result
set projectFolder to choose folder with prompt "SELECT PROJECT FOLDER for " & projCode

set prepend to text -5 thru -1 of projCode
tell application "System Events" to set name of projectFolder to (prepend & " - " & (get name of projectFolder))

Bingo! Thanks so much for your help!