Script works fine in Script Editor, but not as an application

It’s been a while since I’ve done any scripting and lately I’ve been trying to update some of my old OS 9 scripts. One of these scripts copied the icon of an item in the Finder and created a new folder with that same icon. I figured out a OS X work-around for this script with UI scripting. (Bear with my scripting… I’m still a novice.)
The problem I’m having is that this script works fine when choosing run from within Script Editor, but when I save it as an application I get an error saying that, “This operation could not be completed.” I can also get this to work fine if I cut out everything but the “Deal with disk” section.

What’s keeping this script from working outside of Script Editor or with the “if statements” included?

(Replace StartupDisk with the name of your startup disk and UserName with your user name)

tell application "Finder"
	activate
	set this_item to the selection as alias
	set its_name to the name of this_item
	set new_name to its_name & " copy"
	set item_properties to properties of this_item as list
	set first_item to the first item of item_properties as string
	--Deal with a disk
	if first_item is "disk" then
		tell application "System Events" to tell process "Finder"
			keystroke "i" using command down
			delay 0.5
			keystroke tab
			delay 0.5
			keystroke "c" using command down
			delay 0.5
			keystroke "w" using command down
			delay 0.5
		end tell
		set new_item to "*StartupDisk*:Users:*UserName*:Desktop:" & new_name as string
		make new folder with properties {name:new_name}
		select item new_item
		tell application "System Events" to tell process "Finder"
			keystroke "i" using command down
			delay 0.5
			keystroke tab
			delay 0.5
			keystroke "v" using command down
			delay 0.5
			keystroke "w" using command down
			delay 0.5
			keystroke "w" using command down
			delay 0.5
		end tell
	else
		set container_path to container of this_item as string
		set new_item to container_path & new_name
		make new folder at container_path with properties {name:new_name}
		--Deal an item on the desktop
		if the last word of container_path is "Desktop" then
			tell application "System Events" to tell process "Finder"
				keystroke "i" using command down
				delay 0.5
				keystroke tab
				delay 0.5
				keystroke "c" using command down
				delay 0.5
				keystroke "w" using command down
				delay 0.5
			end tell
			select item new_item
			tell application "System Events" to tell process "Finder"
				keystroke "i" using command down
				delay 0.5
				keystroke tab
				delay 0.5
				keystroke "v" using command down
				delay 0.5
				keystroke "w" using command down
				delay 0.5
				keystroke "w" using command down
				delay 0.5
			end tell
		else
			--Deal with an item in any other folder
			tell application "System Events" to tell process "Finder"
				keystroke "i" using command down
				delay 0.5
				keystroke tab
				delay 0.5
				keystroke "c" using command down
				delay 0.5
				keystroke "w" using command down
				delay 0.5
			end tell
			select item new_item
			tell application "System Events" to tell process "Finder"
				keystroke "i" using command down
				delay 0.5
				keystroke tab
				delay 0.5
				keystroke "v" using command down
				delay 0.5
				keystroke "w" using command down
				delay 0.5
			end tell
		end if
	end if
end tell

Hi,

take a look at this thread with a similar topic,
hope it helps

http://bbs.applescript.net/viewtopic.php?id=18973

Unrelated, but you can replace this.

set new_item to "*StartupDisk*:Users:*UserName*:Desktop:" & new_name as string

.with this:

set new_item to (path to desktop as Unicode text) & new_name

Stefan and Bruce, thanks for the help. :cool:

I probably should have done a better job of explaining exactly what I wanted to do with this script in my first post. I wanted to make a folder that had the same icon and name (plus the word “copy”) of any item, at the same location of that item. Anyway… I was running into trouble when dealing with volumes mounted on the desktop. I reduced the amount of variables and came up with a much cleaner script that works.

But, I still don’t understand why the original script gave me errors when saved as an application, yet worked fine when I ran it through Script Editor.

Can someone explain this?

Here’s the new script that works…

tell application "Finder"
	activate
	set this_item to the selection as alias
	set new_name to the name of this_item & " copy"
	open information window of this_item
	tell application "System Events" to tell process "Finder"
		keystroke tab
		keystroke ("c" & "w") using command down
	end tell
	--Deal with a disk
	if the kind of this_item is "Volume" then
		make new folder at desktop with properties {name:new_name}
		open information window of (folder new_name of desktop)
		tell application "System Events" to tell process "Finder"
			keystroke tab
			keystroke ("v" & "w") using command down
		end tell
		--Deal with any other item
	else
		set container_path to container of this_item as string
		set new_item to container_path & new_name
		make new folder at container_path with properties {name:new_name}
		open information window of (new_item as alias)
		tell application "System Events" to tell process "Finder"
			keystroke tab
			keystroke ("v" & "w") using command down
		end tell
	end if
end tell

I have the same issue with this have securityspy (http://www.bensoftware.com/ss/index.html) and sms mac(http://www.smsmac.com/en/applescript/)
the script i wrote and copied works if i push the run botten but when i save as run only it dont work

tell application “SMS Mac Scripting”
set account to “xxxxxxxxxxx@bellsouth.net
set pin to “xxxx”
set m to make new outgoing sms with properties {flash:false}
tell m
set message to “motion detected!”
set recipients to {“+1xxxxxxxxxxx”, “+1xxxxxxxxxx”}
set sender to “+1xxxxxxxxxx”
set delivery report to true
try
send
on error errMsg number errNum
display dialog "Error: " & errNum & ". " & errMsg
end try
end tell
end tell
x is private numbers

same error when i save as run only with security spy as without
unable to read file because script is not editable( it was saved as run only)
enable access for assistive devices is on

Model: dual g4 1 ghz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)