Noobie Tightening up the file renaming code

My script will require several user inputs to build a file name before renaming a file. In a previous post I got some great advice on the renaming part. I got the string built up… pretty long and ugly so thought I would take a part of it to get some suggestions on how to tighten it up. In particular I want to learn about using globals or not. Murphy is helping me out… as I pulled out the short piece, it is no longer working.

global streetRet
global dateStr
setStreet()
set ddate to date string of (current date)
date_format(ddate)
setFileName(streetRet, dateStr)

on setStreet()
	set aStreet to {"W_30", "W_31", "W_32", "W_33", "W_34"} as list
	set streetRet to choose from list aStreet with prompt "Which Street?" without multiple selections allowed and empty selection allowed
	return streetRet
end setStreet

to date_format(old_date) -- Old_date is text, not a date.
	set {year:y, month:m, day:d} to date old_date
	tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "_" & text 5 thru 6 & "_" & text 7 thru 8
	set dateStr to result
	return dateStr
end date_format

on setFileName(strx, stry)
	set nameRet to strx & "_" & stry
	set nameRet to display dialog "File will be renamed!" default answer nameRet buttons {"OK"} default button 1
	set namRet to text returned of nameRet
end setFileName

Model: MBP
Browser: Firefox 32.0
Operating System: Mac OS X (10.8)

Nice script!

Edityed: note that your global declarations are not a part of your run handler.

Hi,

the failure reason is the result of choose from list which is “ as the name implies - a list.
The solution is to get the first item of the list
You don’t need globals if you pass the parameters through the handlers


set streetRet to setStreet()
set ddate to date string of (current date)
set formattedDate to date_format(ddate)
setFileName(streetRet, formattedDate)

on setStreet()
	set aStreet to {"W_30", "W_31", "W_32", "W_33", "W_34"} as list
	return first item of (choose from list aStreet with prompt "Which Street?" without empty selection allowed)
end setStreet

to date_format(old_date) -- Old_date is text, not a date.
	set {year:y, month:m, day:d} to date old_date
	tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "_" & text 5 thru 6 & "_" & text 7 thru 8
	return result
end date_format

on setFileName(strx, stry)
	display dialog "File will be renamed!" default answer strx & "_" & stry buttons {"OK"} default button 1
	set namRet to text returned of result
end setFileName

Hello

Choose from list doesn’t exit like choose file of choose folder when we press the Cancel button.
So, it would be cleaner to edit the setStreet handler this way :

on setStreet()
	set aStreet to {"W_30", "W_31", "W_32", "W_33", "W_34"} as list
	set maybe to (choose from list aStreet with prompt "Which Street?" without empty selection allowed)
	if maybe is false then error number -128
	return first item of maybe
end setStreet

Yvan KOENIG (VALLAURIS, France) vendredi 12 septembre 2014 11:42:29

I would write the streetret() handler like this:

Edit: see Yvan’s example, we both had the same script :cool:

This way the choose from list dialog works pretty much the same as a display dialog. When the user presses cancel the script will be stopped with a general user canceled script error and stops the execution of the entire script. All examples stops executing the script but I think that an error message that actually tells what happened than an error message that says that he can’t get item 1 of false is better.

The posts have been very helpful and I now have a script which builds a good file name, allows a final edit and then renames the file. To make the script more flexible I have gone back to the top. The script assumes you selected the file before running it. If not, it simply quits. It would be better if on finding no file selected, it prompted the user to select a file. So I gave it a try and several more tries with always the same bad result.

tell application "Finder" to set sel to selection -- returns a list
if sel is {} then -- if nothing is selected prompt the user to select a file
	tell application "Finder" to set sel to selection of (choose file with prompt "Select file to rename.")
end if

So it seems that telling Finder to set sel to selection is not at all the same as telling Finder to set sel to selection when the choose command is used. This seems a bit non-intuitive.

you probably mean this, but this opens an extra Finder window


tell application "Finder" to set sel to selection -- returns a list
if sel is {} then -- if nothing is selected prompt the user to select a file
	set chosenFile to (choose file with prompt "Select file to rename.")
	tell application "Finder" to select chosenFile
end if

selection is a property that can be either set or get. But a syntax like set sel to selection of … is wrong because choose file in your case doesn’t contain a property named selection. In your case you want to set the selection property which should be like set selection of … If you want to set the selection property of the application Finder. Stefan is using the select command which is fine as well, but it’s not wrong to use the selection property either.

tell application "Finder"
	if selection is {} then set selection to (choose file with prompt "Select file to rename.")
	set sel to selection --if you need sel further in your script. 
end tell