Objective: Person selects from a list of 7 items from the installer. Any previous version is removed. Any of the choices will be renamed after moving. This renaming is not working for me for some reason. The file is moved without problem, just not renamed. I’m sure it is due to failed basics on my part. lol I confess I do not use applescript as often as I should.
What am I doing wrong?
I’ve tried removing the ‘try’ so both the moving and renaming are on the line below the other. At most the error I get is ‘Can’t make “ABs.app” into type integer.’
I’ve simplified the example as much as possible and kept the file and script reference to it the same.
set apps_path to (path to applications folder from local domain) as string -- destination to applications folder
try
do shell script "rm -r /Applications/ABC.app" -- checks to see if they already had ABC installed and removes it if they did
end try
if (chosenItems contains "ABs") then
tell application "Finder" to duplicate ABs to apps_path with replacing
try
set the name of file "ABs.app" of apps_path to "ABC.app"
end try
end if
-- This does not work. Gets a zero error message.
-- --> application file "ABs.app" of folder "Applications" of startup disk
-- --> error number 0
Model: 2008 macpro
AppleScript: 2.3
Browser: Firefox 16.0.2
Operating System: Mac OS X (10.6)
It looks to me like you’re just having referencing problems. If you create a file-path and coerce to an alias you get immediate feedback if there’s something wrong with the reference (barring errors which will still resolve to an actual item).
I’d rather see you move the app to the Trash than delete it outright with a shell script; it’s generally more user-friendly.
Keep in mind that try”end-try blocks don’t return errors. You have to add in the error statement and error handler. I’ve done this in the script below, although I deliberately withheld the error-block on the file-test - so it will do its job or fail silently.
I wrote my own version of your described installer, so I could reproduce all the referencing. The installer script is in with the 7 items and uses itself as a reference to locate the other files.
Anyhow. There may be some bits you can use.
try
set appsFolder to (path to applications folder)
set _installer to path to me
tell application "Finder"
activate
try
((appsFolder as text) & "ABC.app") as alias
delete result -- Moves to Trash
end try
set installerPath to parent of _installer as alias
set installList to (name of files of installerPath whose name starts with "AB")
set theChoice to choose from list installList
if theChoice ≠false then
set theChoice to ((installerPath as text) & (item 1 of theChoice)) as alias
set theApp to duplicate theChoice to appsFolder with replacing
set name of theApp to "ABC.app"
update appsFolder
end if
end tell
on error e number n
set e to e & return & return & "Num: " & n
tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
if button returned of dDlg = "Copy" then set the clipboard to e
end try
Thank you to everybody. I tried all 3 techniques and all worked well.
I like ccstone’s method of doing a list installer, it is much cleaner and effective than the technique I was using and with a lot less script. So simplistic yet so very effective.
I just needed to list the specific names and extensions otherwise an error by attempting to list the script files also for each of the apps. I will rename the apps more meaningfully so they are easier to understand in the menu. I was previously pre-defining the list names.