【问题标题】:Mavericks AppleScript count every desktop always returns 1Mavericks AppleScript 计数每个桌面总是返回 1
【发布时间】:2014-01-31 20:50:23
【问题描述】:

我有一个取自GitHub 的脚本,它应该根据一天中的时间将每个桌面的壁纸设置为特定图像。 (我已经从原始代码修改它以包含更多时间范围,两个版本中都显示问题)

脚本会尝试计算桌面数量,以便不仅仅更改当前桌面。它首先告诉System Events 以下内容

set theDesktops to a reference to every desktop

然后,为了遍历每个桌面,它会执行以下操作:

if ((count theDesktops) > 1) then
    repeat with x from 2 to (count theDesktops)
        --some code removed, see full code below
    end repeat
end if

问题在于count theDesktops 总是返回 1,无论有多少桌面,如下面的屏幕截图所示。

http://ss.kobitate.com/2013-12-28_0922_2.png

可以做些什么来解决这个问题?这是完整的代码

(*

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 > 6 and h < 8) then
    set periodOfDay to morningEarly
else if (h ≥ 8 and h < 10) then
    set periodOfDay to morningLate
else if (h ≥ 10 and h < 12) then
    set periodOfDay to afternoonEarly
else if (h ≥ 12 and h < 16) then
    set periodOfDay to afternoonLate
else if (h ≥ 16 and h < 18) then
    set periodOfDay to eveningEarly
else if (h ≥ 18 and h < 20) then
    set periodOfDay to eveningLate
else if (h ≥ 20 and h < 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:" & 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) > 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

编辑:修正了我在代码中发现的一个不相关的错误

【问题讨论】:

  • 我认为“桌面”是指实际显示
  • 啊,是的,我现在看到了。只有通过模拟键盘快捷键,我才能找到通过每个桌面使用 AppleScript 选项卡设置每个桌面的墙纸的唯一方法……这对我不起作用,因为该脚本会经常运行以检查时间。
  • 是的,我担心没有办法编写脚本。可能是其他人有想法。

标签: macos applescript osx-mavericks


【解决方案1】:

这似乎现在已修复。我记得@KobiTate 发布它时测试过这个。

我现在使用的是 10.9.1,但不确定何时修复

tell application "System Events"
    set theDesktops to count of (a reference to every desktop)
     set theDesktops2 to count every desktop
end tell
log "count of (a reference to every desktop) = " & theDesktops
log "count every desktop = " & theDesktops2

返回:

(count of (a reference to each desktop) = 2)

(计算每个桌面 = 2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2019-10-10
    相关资源
    最近更新 更多