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