I have a fairly involved script that automates installing software from a .dmg if it's already on my system.
* Requires me to find the app on the local disk and open its container folder (which I do very quickly with LaunchBar).
* Mounts the dmg file selected in the Finder.
* Matches the contained app with open windows in the Finder.
* Trashes the local app.
* Replaces it with the new one on the dmg.
At the moment I'm prototyping some code to automate finding the app on the local disk – which will be faster and safer than what I'm doing now.
The script is working as is, so I'm going to share it at this point.
Note – this code doesn't mount the dmg – you have to do that manually.
Enjoy.
------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/03/29 03:33
# dMod: 2017/03/29 03:33
# Appl: Software Installer
# Task: Attempt to find an app on a DMG in the /Applications folder.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Software, @Installer, @DMG
------------------------------------------------------------------------------
use framework "Foundation"
use framework "AppKit"
use scripting additions
------------------------------------------------------------------------------
set theApp to missing value
tell application "Finder"
set frontWinID to id of front window
tell window id frontWinID
set winTarget to its target
if winTarget's class is disk then
set appsOnDiskList to my listFilesByExtension:{"app"} fromDirectory:(winTarget as alias)
else
error "Front window is NOT a disk!"
end if
tell appsOnDiskList
if its length is 1 then
set theApp to (item 1 of it) as alias
else
error "Too many applications found on disk!"
end if
end tell
set appBundleID to id of theApp
end tell
end tell
if theApp ≠ missing value then
set theAppPath to qf(POSIX path of theApp)
set shCMD to "mdfind -onlyin /Applications/ 'kMDItemCFBundleIdentifier == \"" & appBundleID & "\"c'"
set theAppLocalPath to do shell script shCMD
if length of (paragraphs of theAppLocalPath) = 1 then
set theAppLocalAlias to alias POSIX file theAppLocalPath
end if
tell application "Finder"
activate
set localAppParentFolder to parent of theAppLocalAlias as alias
open localAppParentFolder
end tell
end if
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on listFilesByExtension:listOfExtensions fromDirectory:sourceFolder
set sourceFolder to POSIX path of sourceFolder
set fileManager to current application's NSFileManager's defaultManager()
set aURL to current application's |NSURL|'s fileURLWithPath:sourceFolder
set directoryContents to fileManager's contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:0 |error|:(missing value)
set foundItemList to current application's NSPredicate's predicateWithFormat_("pathExtension.lowercaseString IN %@", listOfExtensions)
set foundItemList to directoryContents's filteredArrayUsingPredicate:foundItemList
return foundItemList as list -- furls
end listFilesByExtension:fromDirectory:
------------------------------------------------------------------------------
on qf(_text)
return (quoted form of _text)
end qf
------------------------------------------------------------------------------