Script not repeating the dialog box

tell application "Finder"
	
	set displaydialog to display dialog "Enter  volume number, delimiter separated by a comma" default answer ""
	set thedelimiters0 to the text returned of displaydialog
	set AppleScript's text item delimiters to ","
	set thevolume to first text item of thedelimiters0
	set thedelimiters to second text item of thedelimiters0
	set AppleScript's text item delimiters to ""
	repeat with aitem in (get selection)
		set x to name of aitem
		set theext to name extension of aitem
		set AppleScript's text item delimiters to thedelimiters
		set z to second text item of x
		set z1 to text 1 thru 2 of z
		set z2 to thevolume & "x" & z1 & "." & theext
		
		set theconfirm to display dialog "Accept new name: " & z2 buttons {"ok", "cancel"} default button "ok"
		if button returned of theconfirm is "ok" then
			set name of aitem to z2
		else
			display dialog "bye!"
		end if
	end repeat
end tell

This script works on Finder files which are named like
(volume number: 2, delimiter: 2VL)
Tax notes.2VL03 - Part1.doc --result: 2x03.doc
Tax notes.2VL04 - Part1.doc --result: 2x04.doc
Tax notes.2VL05 - Part1.doc --result: 2x05.doc
Tax notes.2VL06 - Part1.doc --result: 2x06.doc
(volume number: 1, delimiter: 1x)
MICS lecture - 1x04 - Part1.rtf --result: 1x04.rtf
MICS lecture - 1x05 - Part1.rtf --result: 1x05.rtf
MICS lecture - 1x06- Part1.rtf --result: 1x06.rtf
(volume number: 3 delimiter: 3VLS)
History.3VLS04 - Part1.doc --result: 3x04.doc

Problems with this script:

  1. When cancel is clicked, the display dialog “bye!” does not appear.
  2. It does not work properly on multiple files if “cancel” is clicked for any one file.
  3. When the dialog box for “ok”, “cancel” appears, it is frontmost but it still does not accept the return key. The “Finder” in the “Finder” menu bar (near the apple logo in the menu bar) is pressed when the return key is pressed.

don’t use the word ‘cancel’ – see if that works …

set theconfirm to display dialog "Accept new name: " & z2 buttons {"ok", "no"} default button "ok"
if button returned of theconfirm is "ok" then
	set name of aitem to z2
else
	display dialog "bye!"
end if

Tom

Hi,

pressing the button Cancel in display dialog throws an error -128 (user cancelled)


.
try
	set theconfirm to display dialog "Accept new name: " & z2 buttons {"ok", "no"} default button "ok"
	set name of aitem to z2
on error number n
	if n = -128 then display dialog "bye!"
end try
.

Note: display alert doesn’t behave this way

Thanks Tom_X and StefanK.
Any solution/workaround to my third problem?

This is because the dialog box isn’t actually frontmost. Easily solved if you write your dialogs like this…

tell me
	activate
	display dialog "Now I'm frontmost"
end tell

if Hank’s idea doesn’t work, try this - if it doesn’t do the trick, don’t worry, Stefan is awake. :lol:

tell me to display dialog "Enter volume number, delimiter separated by a comma" default answer ""

this might be better…

tell application "Finder"
	tell me
		activate
		set displaydialog to display dialog "Enter volume number, delimiter separated by a comma" default answer ""

Tom

Thanks a lot, again. Problem solved!!