Skip to content

Instantly share code, notes, and snippets.

@napkindrawing
Created November 2, 2012 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save napkindrawing/4002139 to your computer and use it in GitHub Desktop.
Save napkindrawing/4002139 to your computer and use it in GitHub Desktop.
Create Google Chrome App
(*
* Creates an application shortcut in Google Chrome.
* It's derived from Bracken King's shell script, who deserves all the credit for the idea, see
* http://www.lessannoyingsoftware.com/blog/2010/08/149/Create+application+shortcuts+in+Google+Chrome+on+a+Mac
* I just wanted to make this functionality usable for a couple of friends who never want to see a terminal screen.
* This is also my first real AppleScript attempt, so many things could probably be optimized, enhanced, etc. Feel free to do so.
*
* Mait Vilbiks <happy@hot.ee> @2011-01
* Provided AS IS, "Public domain", ie. no licence required for anything you do with this, and don't blame me if it doesn't work either.
* Written and tested on Mac OS X 10.5.8
*)
set err to ""
-- find the Google Chrome application
set gChromeOrigPath to POSIX path of (path to application "Google Chrome") as text
-- parse the folder of the app, this is where we put the new app
set gAppRoot to dirname(get characters 1 thru -1 of gChromeOrigPath as string)
-- the full path to Google Chrome executable
set gChromePath to quoted form of (gChromeOrigPath & "Contents/MacOS/Google Chrome")
-- ask for the name of the new application
set gAppName to ""
set gAppNameTxt to "What should the Application be called?"
repeat until gAppName is not ""
set gAppName to the text returned of (display dialog gAppNameTxt default answer "")
if gAppName is not "" then
if CheckExistence(POSIX file (gAppRoot & "/" & gAppName & ".app")) then
tell application "Finder"
set gAppNameTxt to "an Application called \"" & gAppName & "\" already exists, choose a different name."
set gAppName to ""
end tell
end if
end if
end repeat
-- make the folder structure
try
do shell script "rm -rf /tmp/" & ReplaceText(gAppName, " ", "\\ ")
do shell script "rm -rf /tmp/" & ReplaceText(gAppName & ".app", " ", "\\ ")
end try
tell application "Finder"
set gAppFolder to make new folder at (POSIX file "/tmp") with properties {name:gAppName}
set gContFolder to make new folder at gAppFolder with properties {name:"Contents"}
make new folder at gContFolder with properties {name:"Resources"}
make new folder at gContFolder with properties {name:"MacOS"}
make new folder at gContFolder with properties {name:"Profile"}
end tell
-- ask for the URL
set gAppURL to the text returned of (display dialog "What is the web address?
Provide the full URL, with http://
or a local html file path in the form
file:///Users/myname/Documents/somefile.html" default answer "")
-- App Icon
set gAppIcon to POSIX path of ((gContFolder as string) & "Resources:icon.")
try
set gotIcon to false
repeat until gotIcon
-- ask for an icon image
set gAppIconSrc to choose file with prompt "Select an icon for the new app, it must be a perfect square (width=height)
click Cancel to use the default Chrome icon" of type {"public.jpeg", "public.png", "public.tiff", "com.apple.icns"} without invisibles
tell application "System Events"
set iconUTI to type identifier of gAppIconSrc
end tell
if iconUTI is equal to "com.apple.icns" then
-- a ready made icon file, so just copy it
do shell script "cp -p " & ReplaceText(POSIX path of gAppIconSrc, " ", "\\ ") & " " & ReplaceText(gAppIcon & "icns", " ", "\\ ")
set gotIcon to true
else
-- image size conversion, saving as TIFF
tell application "Image Events"
launch
set this_image to open gAppIconSrc
copy dimensions of this_image to {W, H}
-- not accepting a rectangular image, only square
if W is equal to H then
scale this_image to size 128
save this_image as TIFF in gAppIcon & "tiff"
set gotIcon to true
end if
close this_image
end tell
-- convert the tiff into icon
do shell script "tiff2icns -noLarge " & quoted form of (gAppIcon & "tiff") & " >& /dev/null"
end if
end repeat
on error errText number errNum from frErr to toErr
if (errNum is equal to -128) then
-- Cancel click, so use the Chrome icon
do shell script "cp -p " & quoted form of (POSIX path of gChromeOrigPath & "Contents/Resources/app.icns") & " " & quoted form of (gAppIcon & "icns")
else
set err to "Some error occured while processing the image: " & errText & " (" & errNum & ")"
end if
end try
if err is equal to "" then
-- write the executable
set gExecCont to "#!/bin/sh
iam=\"$0\"
profDir=$(dirname \"$iam\")
profDir=$(dirname \"$profDir\")
profDir=\"$profDir/Profile\"
exec " & gChromePath & " --user-data-dir=\"$profDir\" \"$@\" \"" & gAppURL & "\""
set gExecutable to (gContFolder as string) & "MacOS:" & gAppName
set fp1 to open for access gExecutable with write permission
write gExecCont to fp1
close access fp1
-- set exec permissions
do shell script "chmod 755 " & quoted form of POSIX path of gExecutable
end if
-- write the Info.plist
if err is equal to "" then
set gPlistCont to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>CFBundleExecutable</key>
<string>" & gAppName & "</string>
<key>CFBundleIconFile</key>
<string>icon</string>
</dict>
</plist>
"
set fp2 to open for access ((gContFolder as string) & "Info.plist") with write permission
write gPlistCont to fp2
close access fp2
end if
-- rename it to final .app
if err is equal to "" then
set gApp to gAppName & ".app"
tell application "Finder"
set name of gAppFolder to gApp
move (POSIX file ("/tmp/" & gApp) as alias) to folder (POSIX file (gAppRoot & "/") as alias)
end tell
-- tell user we're done
display dialog gAppRoot & "/" & gAppName & ".app is ready
You can move this new app to any convinient folder.
NOTE!! if you move the Google Chrome.app away from " & gAppRoot & "/, this new app shortcut will stop working and you'll have to remake it again." buttons {"OK"} default button "OK"
else
tell application "Finder" to delete gAppFolder
display dialog err & "
Close this dialog, no harm was done, but no app either." buttons {"OK"} default button "OK"
end if
(*
* functions
*)
-- http://hohabadu.de/?APPLESCRIPT/Praktische-Handler/Datei-oder-Ordner-vorhanden%3F
on CheckExistence(FileOrFolderToCheckString)
try
alias FileOrFolderToCheckString
return true
on error
return false
end try
end CheckExistence
-- http://www.alecjacobson.com/weblog/?p=49
on last_offset(the_text, char)
try
set len to count of the_text
set reversed to reverse of characters of the_text as string
set last_occurrence to len - (offset of char in reversed) + 1
if last_occurrence > len then
return 0
end if
on error
return 0
end try
return last_occurrence
end last_offset
-- http://www.alecjacobson.com/weblog/?p=52
on dirname(the_path)
set last_occurrence to last_offset(the_path, "/")
if last_occurrence is equal to 0 then
return "."
end if
if last_occurrence is equal to 1 then
return "/"
end if
if last_occurrence is equal to (count of the_path) then
set the_path to items 1 thru (last_occurrence - 1) of the_path as string
return dirname(the_path)
end if
return items 1 thru (last_occurrence - 1) of the_path as string
end dirname
-- http://lists.apple.com/archives/Applescript-users/2005/Nov/msg00008.html
on ReplaceText(theString, fString, rString)
set current_Delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to fString
set sList to every text item of theString
set AppleScript's text item delimiters to rString
set newString to sList as string
set AppleScript's text item delimiters to current_Delimiters
return newString
end ReplaceText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment