MacOS – Using osacript to access Finder from crontab (or from non-interactive session)

applescriptcronfindermacosterminal

I have a script that generates a dmg file from my application bundle. This is generated every night from our subversion repository.

If would be interesting to set up things like the DMG's window size, background image, etc, directly from the script. Currently the only way I know to do so is using osascript and calling Finder's applescript functionality.

The problem is that this script is ran from crontab, so there is no graphical session open – obviously it will never find a Finder open.

I copied this snippet from here: http://asmaloney.com/2013/07/howto/packaging-a-mac-os-x-application-using-a-dmg/

# tell the Finder to resize the window, set the background,
#  change the icon size, place the icons in the right position, etc.
echo '
   tell application "Finder"
     tell disk "'${VOL_NAME}'"
           open
           set current view of container window to icon view
           set toolbar visible of container window to false
           set statusbar visible of container window to false
           set the bounds of container window to {400, 100, 920, 440}
           set viewOptions to the icon view options of container window
           set arrangement of viewOptions to not arranged
           set icon size of viewOptions to 72
           set background picture of viewOptions to file ".background:'${DMG_BACKGROUND_IMG}'"
           set position of item "'${APP_NAME}'.app" of container window to {160, 205}
           set position of item "Applications" of container window to {360, 205}
           close
           open
           update without registering applications
           delay 2
     end tell
   end tell
' | osascript

So, is there a way to launch finder without an active graphical session open (that is, someone logged in the machine)? Or is there a way to perform the same, without using applescript and Finder?

Best Answer

The following page describes the process of creating a template, including background, views, etc, and then using a Makefile to update the DMG package with new files anytime you need to. It doesn't use Finder so it should work for batch updates.

https://el-tramo.be/guides/fancy-dmg/