我可以 read/write Applescript 中的 Illustrator 画板名称吗

Can I read/write the name of the Illustrator artboard in Applescript

是否可以在 Applescript 中获取 Illustrator 画板的名称?

在我尝试获取画板的名称之前,此脚本运行良好:

tell application "Adobe Illustrator"
  tell document 1
       set artboards_count to count of artboards
       set c to 1       
       repeat while c <= artboards_count 
          log index of artboard c as text
          log artboard rectangle of artboard c as text 
          log name of artboard c as text -- this line fails
          set c to c + 1
       end repeat
   end tell
end tell

log name of artboard c as text 行失败 - 其他一切正常。

消息是:

Adobe Illustrator got an error: Can’t get name of artboard 1 of document 1. (-1728)

知道为什么吗?

设置名称也失败了,顺便说一句。但是如果我这样做

tell application "Adobe Illustrator"
  tell document 1
       return properties of artboard 1
  end tell
end tell

我得到(为澄清而添加的回车 returns):

artboard rectangle:0.0, 0.0, 841.889999999999, -595.280000000001, 
   ruler PAR:1.0, show center:false, show cross hairs:false, 
   show safe areas:false, ruler origin:0.0, 0.0, name:Artboard 1,
   container:document 1, best type:reference, default type:reference,
   class:artboard, index:1

谁会认为 属性 name 应该在那里。

名称 属性 是只读的,因此一旦画板存在就无法更改它。即使您可以获得画板的属性,如果您只是想针对特定的画板,也无法将其转换为字符串。但是我偶然发现了一种方法可以做到这一点。假设您有多个画板,并且想要定位名为 "Squash this" 的画板。方法如下:

Tell application "Adobe Illustrator"
    tell current document
        set artCount to number of artboards
        repeat with i from 1 to artCount
            set artProp to get properties of artboard i
            try
                set propString to artProp as string --this will fail
            on error
                set errorDisp to text of result --this captures the text of the error
            set errorDispText to errorDisp as string --changes the text to a ¬ 
                searchable string
            end try
            if errorDispText contains "quash" then
                display notification "errorDispText" --oddly enough, this displays just ¬
                  the artboard name 
                exit repeat
            end if
        end repeat
     end tell
end tell

最好的解决办法是强制;获取从 Artboard 属性返回的记录并将记录强制转换为列表。因此,记录中您的名字 属性 成为列表中的第 7 项。下面的代码将获取画板尺寸、名称和索引,并将它们放入列表中以供稍后在您的脚本中使用。

希望对您有所帮助!

tell application "Adobe Illustrator"
    set artboardDetails to {}
    tell front document
        set allArtboards to every artboard
        repeat with i from 1 to count of allArtboards
            set thisArtboard to item i of allArtboards
            set thisArtboardProps to properties of thisArtboard
            set thisArtboardDimensions to artboard rectangle of thisArtboardProps
            set thisArtboardIndex to index of thisArtboardProps

            --WORKAROUND
            set thisArtboardPropsCoerce to thisArtboardProps as list
            set thisArtboardName to item 7 of thisArtboardPropsCoerce

            set the end of artboardDetails to {thisArtboardName, thisArtboardIndex, thisArtboardDimensions}
        end repeat
    end tell
end tell

(*
--WORKAROUND
The following is a workaround for error returned from:
set anArtboardName to the name of anArtboard -- this line will not return a result, just errors out

BUT WE NEED TO WATCH OUT FOR THE COERCED LIST!
So, we have to coerce the properties record of artboard to a list first

WEIRD I KNOW!
The coercion then changes the record of 11 items to a list of 12 items
set anArtboardProps to properties of anArtboard as list
set anArtboardName to item 7 of anArtboardProps

NOW LETS MATCH UP THE COERCION ITEMS
The first lines are from the record properties, and the second lines are from the coerced to list versions.

01. artboard rectangle:{0.0, 768.0, 1366.0, 0.0}, 
01. {0.0, 768.0, 1366.0, 0.0}, 

02. ruler PAR:1.0, 
02. 1.0, 

03. show center:false, 
03. false, 

04. show cross hairs:false, 
04. false, 

05. show safe areas:false, 
05. false, 

06. ruler origin:{0.0, 0.0}, 
06. {0.0, 0.0}, 

07. name:"Artboard 1", 
07. "Artboard 1", 

08. container:document 1, 
08. document 1, 

09. best type:reference, 
09. reference, 

10. default type:reference, 
10. reference, 

11. index:1
11. artboard, 

12. NULL - nothing in the record
12. 1 - so this line is the index of the artboard
*)

这是我的解决方法。由于每次编译时 «class bAl9» 都会消失,因此您每次都必须 copy/paste,但它可以工作。是的,«class bAl9» 将变成“名称”,下次将无法正确编译。感谢 Adob​​e!

tell application "Adobe Illustrator"
    tell document 1
        repeat with x in (every artboard)
            log index of x as text
            log artboard rectangle of x as text
            log «class bAl9» of x as text -- artboard's name property = «class bAl9»
        end repeat
    end tell
end tell

画板的名称属性是read/write,这样重命名画板很方便得到诀窍。

编辑:对于列表中的循环,我总是使用 repeat with x in l 语句。它又聪明又快。

Stu 的回答是我找到的最好的回答。将记录强加到列表中是绝妙的——我什至不知道你可以这样做。我的代码是:

    repeat with i from 1 to the (count of AllArtBoards)
        set ThisArtBoard to item i of AllArtBoards
        set ThisArtBoardName to item 7 of ((properties of ThisArtBoard) as list)
        set the end of AllArtBoardNames to ThisArtBoardName
    end repeat