사과 스크립트를 사용하여 특정 시트에 iWork 번호로 새 테이블 만들기
-
21-08-2019 - |
문제
새 테이블을 새 테이블 또는 현재 선택한 것이 아닌 한 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
이것을 달성하는 데 더 많은 성공을 거둔 사람이 있습니까? 이것은 나에게 숫자의 일부 고급 스프레드 시트 스크립트에 대한 약간의 문제처럼 보입니다.
해결책
확인. 나의 첫 번째 대답은 받아 들여지고 삭제할 수 없었지만 부적절했기 때문에 그것을 편집하고 실제로 문제를 해결하는 코드를 추가하기로 결정했습니다!
이 코드는 Nigel Garvey를 인정하는 Yvan Koenig를 통해 제공됩니다. 둘 다 활성화되어 있습니다 사과 스크립트 사용자 목록, 그건 그렇고,이 두 신사의 활동과 다른 많은 훌륭한 사과 스크립터의 활동 때문에 우수합니다.
그것은 GUI 스크립팅에 의존합니다 (이것은 항상 뒤로 물러나야하는 끔찍한 일이지만 우리가 가진 것입니다).
전화 my activateGUIscripting()
관리자 비밀번호가 나타나게 될 프롬프트가 발생할 수 있습니다. 당신이 절대적으로 알다 GUI 스크립팅 활성화 (System Prefs-> Universal Access-> Assistive Devices에 대한 액세스 활성화)-이 라인을 생략 할 수 있습니다.
다음 두 줄은 예제 호출 이므로이 예제 호출에 "시트 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