Last active
December 17, 2015 01:28
-
-
Save dbyler/5528131 to your computer and use it in GitHub Desktop.
Same as https://gist.github.com/dbyler/5308392, except it automatically chooses `versionedApplicationName` to bless (without prompting for the user's choice)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
property applicationsFolder : "/Applications2" | |
property applicationName : "OmniFocus" | |
property versionedApplicationName : "OmniFocus 1" -- the version to restore | |
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… | |
- Identifies which copy of an app matches the naming scheme of versionedApplicationName | |
- If none, exits | |
- Quits chosen application(s) 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) | |
set chosenVersion to false | |
repeat with thisVersion in InstalledVersions | |
if thisVersion contains versionedApplicationName then set chosenVersion to thisVersion | |
end repeat | |
if chosenVersion is false then return | |
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