AppleScript – Automate Screencast Setup with AppleScript

applescriptautomator

I'm currently looking for an Applescript to help automate my desktop processes, as I do a lot of screencasts and I'm looking for something that would do the following.

  1. hide the dock
  2. hide the top bar
  3. change the wallpaper to a specified wallpaper

is there a place on the web that has automator scripts that would do something like this ?

Best Answer

You didn't say what version of OS X you're using, although I'm assuming it's OS X El Capitan, which is what this was tested under. This is kind of a kludge but it works nonetheless. Using Script Editor and the AppleScript code below, I made an AppleScript application that toggles the state of hiding/unhiding the Dock and Menu bar, as well as changing the wallpaper between the two states.

Here's where the kludge comes in. Because there is not one explicit way, that I know of, to code the script to do everything in the background as easily as toggling the state of the auto hide preference of the Dock and changing the wallpaper, a mix of methods needs to be used. One of which requires giving the application assistive access rights to perform the toggling of the "Automatically hide and show the menu bar" checkbox in the General section of System Preferences.

It'd be nice if something like tell menu preferences to set autohide to not autohide would work the same way it does for the Dock, but it doesn't and why using a UI programmatic method is required to toggle the state of the Menu bar.

Otherwise this could all be done in the background and not having to give the application permission to manipulate the UI. (There actually is a way, however it's not as clean as this way is, in that it requires programmatically killing Finder for the change to the global preferences .plist file to toggle the state of the menu bar to take effect. So I prefer this method as the changes appear more gracefully made, then... bam... done and now wondering what the heck just happened.)

Note: As coded I'm making the assumption that the state of both the Dock and the Menu bar are in sync. In other words, they are both always either showing or hidden together under normal usage and as such this script toggles the state of each to the opposite of the current state prior to its execution. If that is not the case, then additional logic needs to be applied.


In this example code, the wallpaper is set between the OS X El Capitan default and another in its Desktop Pictures collection. Obviously you can choose whatever you want for each state.

The only code you should have to change is the pathname of the image files being used for the Desktop wallpaper.

tell application "System Preferences"
    activate
    reveal pane id "com.apple.preference.general"
    delay 1
end tell

tell application "System Events"
    click checkbox "Automatically hide and show the menu bar" of window "General" of process "System Preferences"
    key code 12 using command down
    tell dock preferences to set autohide to not autohide
    if (get autohide of dock preferences) is false then
        set picture of desktop 1 to POSIX file "/Library/Desktop Pictures/El Capitan.jpg"
    else
        set picture of desktop 1 to POSIX file "/Library/Desktop Pictures/Desert.jpg"
    end if
end tell

To use this code, do the following:

  • Open Script Editor.

  • Copy and paste the code from the Browser to Script Editor.

  • Edit the pathnames of the images to be used for the wallpaper.

  • Save the script as an Application.

  • Open a Finder window to the location you saved the application. (This it to facilitate giving it assistive access in the next instructions.)

  • Open System Preferences and navigate to: Security & Privacy > Accessibility > Privacy

  • Click the lock to make changes.

  • Provide proper credentials and click Unlock.

  • Drag and drop the application from Finder into the "Allow the apps below to control your computer." window and then check the checkbox next to the application's name.

    • Or if you didn't first locate it in Finder, click the [+] button and navigate to the location you saved the app, etc.
  • Close System Preferences.

That's it... It should now be ready to use.