How do I set the icon from an OSX app to a button?

Björnke von Gierke bvg at mac.com
Sun Jan 9 14:28:02 EST 2011


the full approach would be similar to this:

choose app
parse package.app/contents/info.plist for icn file name
convert icn file to png
get image into rev
set icon of button to image

therefore in a LC button, the following works (watch for linewraps).

Note: xml parsing is super simplified, and won't work with accented chars (needs conversion from utf8). Also, some type of apps do not have the icon stored in the info.plist, the fallback would be to try <appbundlename>.icns, or maybe the name of the actual executable in the "MacOS" folder. furthermore, some apps are carbon, no idea if they follow the same rules.

on mouseUp
   lock screen

   --select app
   answer file "choose an app:"
   if it = "" then
      beep
      exit mouseUp
   end if

   --parse plist
   put it & "/Contents/" into theAppPath
   put url ("file:" & theAppPath & "/Info.plist") into theXML
   put offset("CFBundleIconFile", theXML) into theOffset
   repeat two times
      put offset(">", theXML,theOffset) + theOffset into theOffset
   end repeat
   put char theOffset +1 to offset("<", theXML,theOffset) + theOffset -1 of theXML into theName
   if char -5 to -1 of theName <> ".icns" then
      put ".icns" after theName
   end if
  
   --create image paths
   put theAppPath & "Resources/" &  theName into theIconPath
   put specialfolderpath("temp") & "/temp.png" into theImagePath
   
   --run shell app
   put shell("sips -s format png -z 32 32" &&  theIconPath && "--out" && theImagePath) into theError
   if word 1 of theError <> theIconPath then --word 2 should be theImagePath
      beep
      put theError
      exit mouseUp
   end if
   
   --now let rev import
   import paint from file theImagePath
   put the id of last image into lastImport
   set the visible of image ID lastImport to false
   set the icon of me to lastImport
end mouseUp




More information about the use-livecode mailing list