Using droplet variables outside the on open loop

I wrote a file renamer in basic applescript and wanted to adapt it as one of my first applescript studio projects. It’s a droplet application, which renames dropped files sequentially, using user input for the filename prefix and start number. I created a window in mainmenu.nib with variable inputs in text fields and a button linked to a handler which runs the main script. I’ve now realised that the dropped files are put in the “names” variable in the “on open” loop but this variable cannot be passed to the “on clicked” loop. I get an error saying that names is undefined.

I have tried everything to get this to work, including trying to put the “on clicked” loop nested in “on open” but nothing seems to do it. The answer is probably obvious to the experienced but I can’t see it. How can I rewrite this so that I can use the dropped files in the main script, using user inputs from the window I’ve created?

I know this script works perfectly well as a simple droplet, but I’m using it as a starting point for studio apps and what I’m trying to do - use a UI window in a droplet - must be very common.

Help greatly appreciated.


on idle
	(* Add any idle time processing here. *)
end idle

on open names
	
end open

on clicked theObject
	tell window of theObject
		
		set fileList to every file in names
		set fileLabel to contents of text field "prefix" as string
		set startNumber to contents of text field "number" as number
		
		
		repeat with curFile in fileList
			set nameLabel to the name of curFile
			set nameCount to (count nameLabel)
			set fileExtension to text (nameCount - 2) thru nameCount of nameLabel
			set the name of curFile to (fileLabel & "_" & startNumber & "." & fileExtension)
			set startNumber to (startNumber + 1)
			
		end repeat
		
	end tell
	
end clicked


on should quit after last window closed theObject
	return true
end should quit after last window closed


Hi,

use a property, for example


property droppedItems : {}

on open names
	copy names to droppedItems
end open

.

the property is accessible in every handler

Thanks very much. That’s cured the variable error

I’m struggling now with file aliases now. I’ve rewritten as below. There are no errors any more, but when I click the button nothing happens, and no files are renamed:


property droppedItems : {}

on idle
	(* Add any idle time processing here. *)
end idle

on open names
	copy names to droppedItems
end open

on clicked theObject
	tell window of theObject
		
		set fileList to every file of droppedItems
		set fileLabel to contents of text field "prefix" as string
		set startNumber to contents of text field "number" as number
		
		repeat with curFile in fileList
			
			set nameLabel to the name of alias of file curFile
			set nameCount to (count nameLabel)
			set fileExtension to text (nameCount - 2) thru nameCount of nameLabel
			set the name of curFile to (fileLabel & "_" & startNumber & "." & fileExtension)
			set startNumber to (startNumber + 1)
			
		end repeat
		
	end tell
	
end clicked

on should quit after last window closed theObject
	return true
end should quit after last window closed

Not sure what your problem with the script is now.

I maybe wrong But your property droppedItems, will need to be reset at the end of the run, Properties normally retain their values.
Even after the app has quit and relaunched

Hi,

your script cannot work. AppleScript itself can’t change a name of a file, only the Finder or System Events can.

Try this


property droppedItems : {}

on idle
	(* Add any idle time processing here. *)
end idle

on open names
	copy names to droppedItems
end open

on clicked theObject
	tell window of theObject
		
		set fileLabel to string value of text field "prefix"
		set startNumber to integer value of text field "number"
		
		repeat with curFile in droppedItems
			set fileExtension to name extension of (info for curFile)
			if fileExtension is missing value then
				set fileExtension to ""
			else
				set fileExtension to "." & fileExtension
			end if
			tell application "Finder" to set name of contents of curFile to (fileLabel & "_" & startNumber & fileExtension)
			set startNumber to startNumber + 1
		end repeat
	end tell
	
end clicked

on should quit after last window closed theObject
	return true
end should quit after last window closed

Not in AppleScript Studio :wink:

Thanks Stefan, that’s great - works perfectly.

One last favour…can you recommend a decent textbook for Applescript studio? I’ve used the missing manual, which is good for a beginner in basic Applescript but I’ve struggled to find one for the studio.

Once again, your help on this is much appreciated.

the best “book” is Apple’s documentation

http://developer.apple.com/documentation/applescript/conceptual/studiobuildingapps/chapter01/studio_intro_book.html

Everything I learned about AppleScript Studio I learned from this site.:slight_smile:

Ha, I really need to start learning it again… thanks