« Load RAW image on Mac… | Home | MBS FileMaker Plugin,… »

Execute defaults command with Shell functions

Since we introduced the Shell functions in MBS FileMaker Plugin, we find new uses each day. The following example uses the defaults command line tool to add preferences for the Dock application:

Set Variable [ $error ; Value: "" ] 

Set Variable [ $result ; Value: "" ] 

Set Variable [ $shell ; Value: MBS( "Shell.New" ) ] 

Set Variable [ $s ; Value: MBS( "Shell.Execute"; $shell; "/usr/bin/defaults"; "write"; "com.apple.dock"; "persistent-apps"; "-array-add"; "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/Font Book.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>") ] 

Set Variable [ $s ; Value: MBS( "Shell.Wait"; $shell; 2) ] 

Set Variable [ $error ; Value: $error & MBS( "Shell.ReadErrorText"; $shell; "UTF-8") ] 

Set Variable [ $result ; Value: $result & MBS( "Shell.ReadOutputText"; $shell; "UTF-8") ] 

Set Variable [ $s ; Value: MBS( "Shell.Execute"; $shell; "/usr/bin/killall"; "Dock") ] 

Set Variable [ $s ; Value: MBS( "Shell.Wait"; $shell; 2) ] 

Set Variable [ $error ; Value: $error & MBS( "Shell.ReadErrorText"; $shell; "UTF-8") ] 

Set Variable [ $result ; Value: $result & MBS( "Shell.ReadOutputText"; $shell; "UTF-8") ] 

Set Field [ Shell::Error ; MBS( "Text.ReplaceNewline"; $error; 1) ] 

Set Field [ Shell::Output ; MBS( "Text.ReplaceNewline"; $result; 1) ] 

Set Variable [ $r ; Value: MBS("Shell.Release"; $shell) ] 

 
Please note that this is the hard way to do this. I think Apple has an official API for adding dock icons and I may add it to the plugin if needed. 
As you see you need to specify shell tools with full path, so we pass "/usr/bin/defaults" and not just defaults. Also all parameters are passed as separate parameters to the Execute call. Next we run the killall tool on the same shell to kill the dock application and have it restart automatically.
21 12 17 - 11:21