我有麻烦上使用AppleScript特定片创建一个新的表,除非我想要的片插入新的表中是新的或当前选中。 我使用的代码的一般形式是:

    tell application "Numbers"
    tell document 1
    tell sheet "This is the sheet I want to use"
    make new table with properties {name:"A new table"}
    end tell 
    end tell 
    end tell

有没有人有在实现这一目标的任何更大的成功?这在我看来就像一个有点问题,在数一些先进的电子表格的脚本。

有帮助吗?

解决方案

行。我的第一个答案已经被接受,无法删除,但是是不够的,所以我决定把它添加一些代码,确实解决问题!

此代码来经由伊凡柯尼希,谁学分奈杰尔加维。两者都是在 AppleScript的用户列表,这是优秀的,顺便说一句,尤其是因为这两位先生,和许多其他大AppleScripters的活性。

这依赖于GUI脚本(这始终是有依傍一个可怕的事情,但它是我们所拥有)。

my activateGUIscripting()呼叫可能引起的提示管理员密码出现。如果你绝对的了解您已经GUI脚本启用(系统Prefs->通用访问 - >启用辅助设备的访问) - 那么你可以省略这一行

在接下来的两行只是示例调用,所以需要一个称为片材“表1”对这些示例调用工作。

这将允许你在任何文件中任何片创建表,不管是什么选择或最前面。

--EXAMPLE CALLS
my activateGUIscripting()
my selectsheet(1, "Sheet 1")
my createNewTable(1, "Sheet 1", "myNewTable", 69, 13)

--NUMBERS UTILS
on createNewTable(dName, sName, newTable, nb_rows, nb_columns)
    tell application "Numbers" to tell document dName to tell sheet sName
        make new table with properties {name:newTable, row count:nb_rows, column count:nb_columns}
    end tell
end createNewTable

--=====
on activateGUIscripting()
    (* to be sure than GUI scripting will be active *)
    tell application "System Events"
        if not (UI elements enabled) then set (UI elements enabled) to true
    end tell
end activateGUIscripting
--=====
(*
==== Uses GUIscripting ====
 *)
on selectsheet(theDoc, theSheet)
    script myScript
        property listeObjets : {}
        local maybe, targetSheetRow
        --+++++
        -- set log_report to "point 2 : " & (current date) & return
        --+++++
        tell application "Numbers"
            activate
            set theDoc to name of document theDoc (* useful if the passed value is a number *)
            tell document theDoc to set my listeObjets to name of sheets
        end tell -- "Numbers"…

        set maybe to theSheet is in my listeObjets
        set my listeObjets to {} -- So it will not be saved in the script *)
        if not maybe then
            error "The sheet “" & theSheet & "” is unavailable in the spreadsheet “" & theDoc & "” !"
        end if -- not maybe

        set maybe to 5 > (system attribute "sys2")
        tell application "System Events" to tell application process "Numbers"
            tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theDoc
                if maybe then (* macOS X 10.4.x
'(value of attributes contains 0)': '(value of attribute "AXDisclosureLevel" is 0)' sometimes works in Tiger, sometimes not.
The only possible instances of 0 amongst the attributes are the disclosure level of a sheet row and the index of the first row, which represents a sheet anyway.
Another possibility is '(value of attribute -1 is 0)', which makes me uneasy. *)
                    set targetSheetRow to first row where ((value of attributes contains 0) and (value of first static text is theSheet))
                else (* macOS X 10.5.x or higher *)
                    set targetSheetRow to first row where ((value of attribute "AXDisclosureLevel" is 0) and ((groups is {}) and (value of first static text is theSheet)) or (value of first group's first static text is theSheet))
                end if -- maybe…
                (*
Handler modified to accomodate sheets requiring a lot of time to get the focus
*)
                tell targetSheetRow to set value of attribute "AXSelected" to true
                set cnt to 0
                repeat (*
Must way that Numbers becomes ready to receive the value *)
                    try
                        tell targetSheetRow to set value of attribute "AXDisclosing" to true
                        exit repeat
                    on error
                        set cnt to cnt + 1
                        delay 0.5 -- half a second
                    end try
                end repeat
            end tell -- outline…
        end tell -- "System Events"…
        --+++++
        -- set log_report to log_report & "point 3, cnt = " & cnt & return & (current date) & return
        --+++++
        tell application "Numbers" to tell document theDoc to tell sheet theSheet to tell table 1
            with timeout of 20 * 60 seconds (*
WITH this setting, the script will be able to wait 20 minutes for the asked value.
I hope that the document will not be so huge that this delay prove to be too short. *)
                value of cell "A1"
            end timeout
        end tell -- "Numbers"…
        --+++++
        -- set log_report to log_report & "point 4 : " & (current date) & return
        --+++++
        tell application "System Events" to tell application process "Numbers" (*
Do the trick one more time to be sure that the sheet is open *)
            tell targetSheetRow to set value of attribute "AXDisclosing" to true
        end tell -- "System Events"…
        --+++++
        -- return log_report & "point 5 : " & (current date) & return
        --+++++
        (*
End of the modified piece of code
*)
    end script
    run myScript
end selectsheet
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top