This script installs apps from disk images, ZIP files, and package installers (runs installer only). After it copies the app to the Applications folder, it unmounts the disk.
I’ve spent a good amount of time trying to get this right, but I feel there’s still something I can improve. If I test this on a disk image for a second time (consecutively), I get this error (notice the curly quotes):
Here is the script:
global diskInfo, diskName, diskPath, appDisk, action
on getTargetApp(fileExt)
tell application "Finder"
-- find app on disk image
try
get (first file of appDisk whose name extension is fileExt)
on error
get first file of (entire contents of appDisk) whose name extension is fileExt
end try
end tell
end getTargetApp
on getDiskName()
set volOffset to offset of "/Volumes/" in diskInfo
set diskPath to characters volOffset through end of diskInfo as text
set appDisk to POSIX file diskPath as alias
end getDiskName
on diskAction(action, chosenDisk)
do shell script ("hdiutil " & action & space & quoted form of chosenDisk)
end diskAction
tell application "Finder"
set allDMGs to every file of folder (path to downloads folder as text) whose name extension is "dmg"
repeat with eachDMG in allDMGs
set dmgName to name of eachDMG
set dmgPath to POSIX path of (eachDMG as alias)
-- mount and find DMG disk
set diskInfo to diskAction("mount", dmgPath) of me
getDiskName() of me
-- install app from DMG
-- try
installApp() of me
(* on error
-- install package from DMG
try
runInstaller() of me
on error
beep 2
diskAction("unmount", diskPath) of me
end try
end try *)
end repeat
-- install app from ZIP, or run package installer
extractZips() of me
runPkgs() of me
end tell
-- DMG install handler
on installApp()
tell application "Finder"
set theApp to getTargetApp("app") of me
duplicate theApp to folder (path to applications folder as text) with replacing
diskAction("unmount", diskPath) of me
end tell
end installApp
-- DMG installer handler
on runInstaller()
tell application "Finder"
set thePkg to getTargetApp("pkg") of me
open thePkg
end tell
end runInstaller
-- ZIP install handler
on extractZips()
tell application "Finder"
set allZips to every file of folder (path to downloads folder as text) whose name extension is "zip"
if allZips = {} then return
repeat with eachZip in allZips
set zipPath to POSIX path of (eachZip as alias)
do shell script ("unzip -u " & quoted form of zipPath & " -d ~/Downloads/")
end repeat
set newApps to every file of folder (path to downloads folder as text) whose name extension is "app"
move newApps to folder (path to applications folder as text) with replacing
end tell
end extractZips
on runPkgs()
tell application "Finder"
set allPkgs to every file of folder (path to downloads folder as text) whose name extension is "pkg"
repeat with eachPkg in allPkgs
open eachPkg
end repeat
end tell
end runPkgs
Any ideas?