我可以在Applescript中读/写Illustrator画板的名称

时间:2017-11-11 09:55:46

标签: applescript adobe-illustrator

是否可以在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)

知道为什么?

设置名称也失败了,BTW。但是,如果我这样做

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

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

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应该在那里。

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

希望这会有所帮助!

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
*)

答案 2 :(得分:0)

这是我的解决方法。每次编译时,“ bal9类”都会消失,因此每次都必须复制/粘贴,但这是可行的。是的,«class bAl9»将变为“ name”,并且下次将无法正确编译。谢谢Adobe!

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

画板的 name 属性为读/写,一旦获得窍门,就很容易以这种方式重命名画板。

编辑:对于列表中的循环,我总是使用在x中重复l 语句。快速又聪明。

答案 3 :(得分:0)

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