Wallpaper changer script?

Discussion in 'Mac Programming' started by Riku7, Mar 1, 2014.

  1. Riku7 macrumors regular

    Feb 18, 2014
    Skip this background info to get straight to the point :D Okay here's the deal;
    I recently rediscovered Mission Control and created different environments for my very different types of work. The background plays an important role for setting the right mood. Assigning apps to work in certain environments only seemed like a good idea at first, but it didn't work nearly as smoothly as I'd hoped – sometimes the apps kept bouncing to places I didn't mean them to go to, and all the jumping made my concentration and the computer's CPU drain.

    Back to the start. Maybe I'll just handle app and file organizing by using tiny concentration apps, they're great.
    But, the desktop issue.

    I'm fairly new to applescript and automating. I've managed to customize and make a few scripted items for my frequent use, but I can't seem to find a tutorial that's similar enough so I could figure this one out.

    Currently I have 7 special work environments that are mandatory to be kept separate. The amount might change by a few, +-.
    I have a "Mode wallpapers" folder that contains folders that are named after each environment. Those folders contain multiple desktop pictures each.
    What I'd want an easily accessible script to do is to allow me to pick an environment, and it would set the desktop wallpaper by picking a random image in the corresponding environment wallpaper folder.
    Having to open Preferences > Desktop&Screensaver > Specify wallpaper folder, is a workflow killer. Getting a random wallpaper each time promotes creativity and fresh thinking.

    I found some tutorials but they were too different for me to figure out how to edit for getting what I want to do. :(

    For example, this one gave me a nice idea about a chance to have a dialog box with buttons I didn't even know about: http://hints.macworld.com/article.php?story=20090505222800665
    How cool, but when I tried to change the number 3 to 7, the script informed me that I could only have a maximum of 3 buttons. Okay, I could compromise and live with grouping similar environments to threes and making a separate script for each group. But then I couldn't figure out how to change the code to not choose a static specified file, but a random file in a folder.

    Then I came across this one: https://github.com/pipwerks/OS-X-Wallpaper-Changer
    Okay cool, it's set to choose random images in a folder instead of specified ones. Also, it uses separate folders to fetch the files from, and there are several options.
    But the code regarding multiple display handling (which I don't need) was overwhelming, as well as the stuff about changing the wallpaper according to time. I was trying to customize my script by modifying parts of these but because I don't have much experience with code language and search engines only brought somewhat similar reference articles, I'm out of ideas. Any help, please? :confused:
  2. kryten2, Mar 2, 2014
    Last edited: Mar 2, 2014

    kryten2 macrumors 6502a

    Mar 17, 2012
    Probably not the answer you're looking for but you can always cycle through your desktops/spaces using key code in your script. The example in the link can be changed to your needs to include picking a random image.

    Info : https://discussions.apple.com/thread/5670099?tstart=0

    Note : Based on a quick search it simply may not be possible to do what you want, could be wrong though. It (OS-X-Wallpaper-Changer)doesn't work for me. See mavericks-applescript-count-every-desktop-always-returns-1 and how-to-get-a-list-of-the-desktops-in-applescript

    Edit : Try this :

    This script assumes:
    1. You have a folder named "Wallpapers" in your Pictures folder
    2. You have 4 subfolders inside "Wallpapers", with names that match the variables below. 
       * If you decide to use different folder names, you must change the variables to match the new folder names
    3. You have images inside each folder
    For example:
    set privateFolder to "Private"
    set workFolder to "Work"
    set funFolder to "Fun"
    set webFolder to "Web"
    -- List of desktops you have. Names are cosmetic. Numbering is important! If you have 7 then both lists contain 7 items! Keep the space between the number and name!!!!
    set myList to {"1 Private", "2 Work", "3 Fun", "4 Web"}
    -- List of keys to use. Meaning keys 1 to 4 on the numeric keypad
    set myKeyCodes to {83, 84, 85, 86}
    -- Test if lists have equal amount of items. If not display alert and stop the script.
    if (count myList) is not equal to (count myKeyCodes) then
    	display alert "Both lists need to contain the same number of items!" message "Check the number of items of the  list variables in the script." as warning giving up after 5
    end if
    choose from list myList with title "Desktops/Spaces"
    if the result is not false then
    	set myChoice to item 1 of the result
    	if myChoice contains "Private" then
    		set folderName to privateFolder
    	else if myChoice contains "Fun" then
    		set folderName to funFolder
    	else if myChoice contains "Work" then
    		set folderName to workFolder
    	else if myChoice contains "Web" then
    		set folderName to webFolder
    	end if
    	set myChoiceNumber to do shell script "echo " & myChoice & " | awk '{ print $1}'"
    	switchToDesktop(item (myChoiceNumber as number) of myKeyCodes, folderName, first item of myKeyCodes)
    end if
    on switchToDesktop(keyCode, picturesFolder, firstDesktop)
    	tell application "System Events"
    		-- Switch to desktop chosen fron the choose from list window
    		-- using command down depends on see thumbnail
    		key code keyCode using command down
    		delay 0.5
    		-- Set the background picture
    		set picture of current desktop to POSIX path of (my getImage(picturesFolder) as alias)
    		delay 0.7
    		-- Switch back to the first desktop
    		key code firstDesktop using command down
    	end tell
    end switchToDesktop
    -- helper function ("handler") for getting random image
    on getImage(folderName)
    	tell application "Finder"
    		--return some file of folder folderName
    		return some file of folder ((path to pictures folder as text) & "Wallpapers:" & folderName)
    	end tell
    end getImage

    Attached Files:

  3. Riku7, Mar 3, 2014
    Last edited: Mar 3, 2014

    Riku7 thread starter macrumors regular

    Feb 18, 2014
    Oh yeah, the thing with that is, I experienced Mission Control to slow down my computer a lot and all the jumpy action was visually more distracting than useful, so the whole point was to avoid involving Mission Control in this thing altogether. :/

    I actually did manage to figure it out after long hours of learning. Newbie power! :p

    Because it was the people who discussed their script issues openly enough to allow search engines to lead me to all the useful fragments of information, I'll join the club and share my resolution, in case that someone might find it useful too:

    I found a free, very customizable app called ControlPlane. I actually see lots of potential in it already so I keep coming up with new things I can assign it to do for me for a smoother workflow.
    In a nutshell, first you define different context names to ControlPlane.
    Next, you define which evidence sources the app should keep an eye on.
    Then you define rules for what kind of events detected from an evidence source trigger the app to consider a switch of context.
    Last, you define consequences for what automatic actions the app should perform when you either arrive or depart from a context.

    Among everything else, there's an action called "open file" which I'm currently finding the most interesting, because it gives the opportunity to assign an applescript to run when entering or departing a context. The possibilities of what to do with it are almost endless.

    Well, for my issue:
    I created folders based on each context's name.
    Then I moved appropriate collections of wallpaper images to each folder.

    Then I wrote and saved this applescript as a file:
    tell application "Finder"
    set desktop picture to some file of folder {"[COLOR="Magenta"]Volume:Folder:Wallpapers:Folder[/COLOR]"}
    end tell
    I saved as many versions of this script as there are contexts, because obviously the path has to lead to the target context wallpaper folder.
    If someone wants to copy this script and use it, change the pink colored string to match the destination folder path on your computer.

    Next, I assigned ControlPlane to open file (the script file), on arrival to a context.
    Surprisingly simple and it works steadily. Now every time I enter any new context, it picks a random wallpaper from a context-specific wallpaper folder.

    ...My current worry is about whether it'll ever be possible to define a different set of dock items for each context. It'd be so much neater if only the apps and document resources that I really need at each work environment would be visible and everything else would be out of sight. But natively the Dock isn't even scriptable in the first place, and coding shell with dockutil didn't get me any steady enough results to be useful. :confused:
  4. Consultant macrumors G5


    Jun 27, 2007
    Why not different user accounts if you want to keep what you are doing separate?

    Use quick account switching.
  5. Riku7 thread starter macrumors regular

    Feb 18, 2014
    Hmmm. I didn't think of that. What's it like for CPU to, say, leave documents or possibly heavy apps open in a different account?
    Although, since this is my personal computer alone, I've always been choosing to install apps and settings and things onto current user only. Of course some activities require shared access to applications, their settings or files – wouldn't separate accounts require quite a lot of reinstalling? And in case of things that can't be shared between all user accounts, there would be duplicates, possibly unsynced ones. :confused:
  6. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Where did you install these apps? If it's in the usual place (/Applications), then they're probably available to all user accounts.

    I suggest creating a simple non-admin account, and trying it out. That is, switch to the new account, try opening apps, saving preferences, etc. and seeing what happens. If it works out, then great. Otherwise you can delete the created account. Either way, you've actually tried it and learned about it.

Share This Page