Note that there are some explanatory texts on larger screens.

plurals
  1. POMavericks AppleScript count every desktop always returns 1
    text
    copied!<p>I've got a script taken from <a href="https://github.com/pipwerks/OS-X-Wallpaper-Changer" rel="nofollow noreferrer">GitHub</a> that is supposed to set the wallpaper of every desktop to a certain image depending on the time of day. (I have modified it from the original code to include more time ranges, issue shows in both versions) </p> <p>The script attempts to count the number of desktops in order to change more than just the current desktop. It does this by first telling <code>System Events</code> the following</p> <pre><code>set theDesktops to a reference to every desktop </code></pre> <p>And then, in order to loop through every desktop, it does the following:</p> <pre><code>if ((count theDesktops) &gt; 1) then repeat with x from 2 to (count theDesktops) --some code removed, see full code below end repeat end if </code></pre> <p>The issues is that <code>count theDesktops</code> always returns a 1, no matter how many desktops there are, as seen in the following screenshot.</p> <p><a href="http://ss.kobitate.com/2013-12-28_0922_2.png" rel="nofollow noreferrer">http://ss.kobitate.com/2013-12-28_0922_2.png</a></p> <p>What can be done to fix this? Here is the full code</p> <pre><code>(* Script by Philip Hutchison, April 2013 http://pipwerks.com MIT license http://pipwerks.mit-license.org/ This script assumes: 1. You have a folder named "Wallpapers" in your Pictures folder 2. You have a subfolder named "Time of Day" in Wallpapers 3. You have six subfolders inside "Time of Day", 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 4. You have images inside each folder For example: /Users/YOUR_USER_NAME/Pictures/Wallpapers/Time of Day/Afternoon Early/image.jpg GeekTool can execute this script for you at specified intervals. Use this line in the command field: osascript ~/Pictures/Wallpapers/Time\ of\ Day/wallpaper.scpt *) -- BEGIN USER CONFIGURATION -- supply folder names set morningEarly to "Morning Early" set morningLate to "Morning Late" set afternoonEarly to "Afternoon Early" set afternoonLate to "Afternoon Late" set eveningEarly to "Evening Early" set eveningLate to "Evening Late" set nightEarly to "Night Early" set nightLate to "Night Late" -- for multiple monitor support. -- set to true to display the same image on all desktops, false to show unique images on each desktop set useSamePictureAcrossDisplays to true -- END USER CONFIGURATION -- get current hour set h to hours of (current date) -- set default periodOfDay set periodOfDay to nightLate -- change value of periodOfDay based on current time if (h &gt; 6 and h &lt; 8) then set periodOfDay to morningEarly else if (h ≥ 8 and h &lt; 10) then set periodOfDay to morningLate else if (h ≥ 10 and h &lt; 12) then set periodOfDay to afternoonEarly else if (h ≥ 12 and h &lt; 16) then set periodOfDay to afternoonLate else if (h ≥ 16 and h &lt; 18) then set periodOfDay to eveningEarly else if (h ≥ 18 and h &lt; 20) then set periodOfDay to eveningLate else if (h ≥ 20 and h &lt; 22) then set periodOfDay to nightEarly else if (h ≥ 22) then set periodOfDay to nightLate end if -- helper function ("handler") for getting random image on getImage(folderName) tell application "Finder" return some file of folder ("Pictures:Wallpapers:Time of Day:" &amp; folderName) of home as text end tell end getImage tell application "Finder" -- wrapped in a try block for error suppression try -- determine which picture to use for main display set mainDisplayPicture to my getImage(periodOfDay) -- set the picture for additional monitors, if applicable tell application "System Events" -- get a reference to all desktops set theDesktops to a reference to every desktop -- handle additional desktops if ((count theDesktops) &gt; 1) then -- loop through all desktops (beginning with the second desktop) repeat with x from 2 to (count theDesktops) -- determine which image to use if (useSamePictureAcrossDisplays is false) then set secondaryDisplayPicture to my getImage(periodOfDay) else set secondaryDisplayPicture to my mainDisplayPicture end if -- apply image to desktop set picture of item x of the theDesktops to secondaryDisplayPicture end repeat end if end tell -- set the primary monitor's picture -- due to a Finder quirk, this has to be done AFTER setting the other displays set desktop picture to mainDisplayPicture end try end tell </code></pre> <p><strong>Edit:</strong> Fixed an unrelated mistake I found in the code</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload