Skip to content

Instantly share code, notes, and snippets.

@dbyler
Last active December 15, 2015 18:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbyler/5308392 to your computer and use it in GitHub Desktop.
Save dbyler/5308392 to your computer and use it in GitHub Desktop.
This script lets you quickly select a version of an application as the primary one by renaming the "blessed" version to [Application name].app and other versions to [Application name]-[version].app.
property applicationsFolder : "/Applications"
property applicationName : "OmniFocus"
property promptBeforeLaunch : false
(*
This script lets you quickly select a version of an application as the primary one by renaming the "blessed" version to [Application name].app and other versions to [Application name]-[version].app.
What it does…
- Prompts you to select which version to 'bless'
- Quits app if running
- Renames all versions of the app to [applicationName]-[version].app
- Uses app metadata to set version, if available
- If no version is given, uses creation date
- If multiple versions would use the same name, only renames one
- Renames 'blessed' version to [applicationName].app
- Launches the blessed version (with prompt if promptBeforeLaunch == true)
# License
Created by Dan Byler (contact: [first-initial][lastname]@gmail.com)
No license. No warranty. It works for me, but USE AT YOUR OWN RISK.
*)
-- Show existing versions
set InstalledVersions to my get_installed_versions(applicationsFolder)
try
-- calling System Events here has a better experience but sometimes hangs for me when SE is unresponsive
-- tell application "System Events" to choose from list InstalledVersions with prompt "Please select a version of " & applicationName & " to bless."
choose from list InstalledVersions with prompt "Please select a version of " & applicationName & " to bless."
set chosenVersion to item 1 of result
on error
return
end try
if chosenVersion is applicationName & ".app" then
if promptBeforeLaunch then
tell application "System Events" to display dialog ("Selected version is already blessed. Launch " & applicationName & " now?")
if button returned of result is "OK" then tell application applicationName to activate
else
tell application applicationName to activate
end if
return
end if
-- Quit application if it's already running
tell application "System Events"
set ProcessCount to count of (every application process whose name is applicationName)
end tell
if ProcessCount > 0 then
tell application "System Events" to display dialog (applicationName & " is currently running. Choose OK to quit " & applicationName & " and continue.")
if button returned of result is "Cancel" then return
repeat
if ProcessCount is 0 then exit repeat
do shell script "osascript -e 'tell application \"" & applicationName & "\" to quit'"
repeat
tell application "System Events"
set NewProcessCount to count of (every application process whose name is applicationName)
if NewProcessCount < ProcessCount then
set ProcessCount to NewProcessCount
exit repeat
end if
delay 1
end tell
if ProcessCount is 0 then exit repeat
end repeat
end repeat
end if
-- Bless new version
set tempName to applicationName & "--selected.app"
my mv(applicationsFolder, chosenVersion, tempName)
my versionize_names()
my mv(applicationsFolder, tempName, applicationName & ".app")
-- Launch it
if promptBeforeLaunch then
tell application "System Events" to display dialog ("Launch " & applicationName & " now?")
if button returned of result is "OK" then tell application applicationName to activate
else
tell application applicationName to activate
end if
-- Subroutines
on is_app_running(appName)
tell application "System Events"
return (count of (application processes whose name is appName)) is not 0
end tell
end is_app_running
on versionize_names()
set shell_script to "
#!/bin/bash
dir=" & applicationsFolder & "
app=" & applicationName & "
cd \"$dir\"
for a in \"$app\"*.app; do
if [ \"$a\" != *\"--selected\"* ] ; then
namebase=$(echo \"$a\" |sed 's/\\(.*\\)\\.app/\\1/')
version=$(mdls -name kMDItemVersion \"$a\" |sed 's/.*\"\\(.*\\)\"/\\1/')
if [ \"$version\" == \"kMDItemVersion-=-(null)\" ] ; then
modtime=$(stat -f '%m' \"$a\")
version=$(date -j -f \"%s\" \"$(echo $modtime)\" +\"%Y-%m-%d\")
fi
final=\"$app $version.app\"
if [[ \"$final\" != \"$a\" && ! -d \"${final}\" ]] ; then
mv -n \"$a\" \"$final\"
fi
fi
done
"
do shell script shell_script
end versionize_names
on get_installed_versions(applicationsFolder)
--Needlessly awkward to avoid issues with AppleScript interpreting newlines
set rawList to do shell script "ls " & applicationsFolder & " |grep '" & applicationName & "'|tr '\\" & "n' ',' |rev |cut -c 2- |rev"
set AppleScript's text item delimiters to {","}
return text items of rawList
end get_installed_versions
on mv(path, myFile, newName)
do shell script "mv \"" & path & "/" & myFile & "\" \"" & path & "/" & newName & "\""
end mv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment