I want to turn this 101678NJ into this 10-1678-NJ. Just adding dashes after every second character and before the last 2. This string of numbers will always be different.
This does not work:
tell application "Adobe Illustrator"
set mySelection to every character of every text frame of document 1
set myDash to "-"
if mySelection = {} then
set after character 2 of selection to myDash
set after character 7 of selection to myDash
end if
end tell
This is more an effort to ‘bump’ your post than to provide a solution…
I don’t use or have Illustrator, so can’t test anything I suggest, but with luck, someone familiar with Illustrator scripting will come along and clean up my mess. These snippets are provided as suggestions only.
It isn’t clear from your post whether you need to change the names of the text frames or the contents of the text frames.
For the very simple text manipulation, try:
set this_text to "101678NJ"
set amended_text to (characters 1 through 2 of this_text) & "-" & (characters 3 through 6 of this_text) & "-" & (characters 7 through 8 of this_text) as text
--> "10-1678-NJ"
This is given assuming your text frames will always be eight characters… no more no less.
–
For grabbing the name or content of all the text frames, you’re likely to need a construct something like this:
tell application "Adobe Illustrator"
set mySelection to every text frame of document 1 as list -- asking instead for name or contents of the text frame rather than simply 'text frame'
end tell
Then, to make a list of amended items:
if mySelection is not {} then
set amended_list to {}
repeat with this_text in mySelection
set amended_text to (characters 1 through 2 of this_text) & "-" & (characters 3 through 6 of this_text) & "-" & (characters 7 through 8 of this_text) as text
set amended_list to amended_list & amended_text
end repeat
As to how to reinsert the amended_list items, I’m currently at a loss, and prefer not to embarrass myself further with a bollixed attempt at it.
Thanks for the suggestion. I think I’m understanding your posting, but Illustrator is giving me nothing in return. I’m not even getting an error message. Dang.
Also, thanks for reminding me to be a little more clear. Yes, I need this script for a series of numbers, each one different. Always 6 digits and 2 letters at the end. Example: 101987HJ, 106745NU, 115643RF, etc.
This script will not give you anything. It is doing everything you ask it to, but the problem is that you are not asking it to return anything. If you need to replace the original data with it, try
tell application "Illustrator"
set this_text to selection
end tell
set clipboard to (characters 1 through 2 of this_text) & "-" & (characters 3 through 6 of this_text) & "-" & (characters 7 through 8 of this_text) as text
tell application "System Events"
keystroke "V" using command down
end tell
I don’t have Illustrator, so I can’t test this. It should work IF Illustrator can read the selection. If not, this should work. Be warned that both versions of your script will erase your clipboard.
tell application "System Events"
keystroke "C" using command down
end tell
set this_text to contents of clipboard as text
set clipboard to (characters 1 through 2 of this_text) & "-" & (characters 3 through 6 of this_text) & "-" & (characters 7 through 8 of this_text) as text
tell application "System Events"
tell application "System Events"
keystroke "V" using command down
end tell
This will remember your clipboard first
set prevClipboard to contents of the clipboard
tell application "System Events"
keystroke "C" using command down
end tell
set this_text to contents of clipboard as text
set the clipboard to (characters 1 through 2 of this_text) & "-" & (characters 3 through 6 of this_text) & "-" & (characters 7 through 8 of this_text) as text
tell application "System Events"
tell application "System Events"
keystroke "V" using command down
end tell
set the clipboard to prevClipboard
Edit: THIS DOES NOT WORK! I didn’t read the clipboard correctly, and I don’t know how to. If anyone does, I’d appreciate it.
hey Peter, you used the English “through” instead of the Applescript-ese “thru”. By specifying numbers from the end range, you can avoid being bound by a set length.
set this_text to "123456789"
set amended_text to (this_text's text 1 thru 2) & "-" & (this_text's characters 3 thru -3) & "-" & (this_text's characters -2 thru -1)
amended_text
Thanks for the suggestions. Unfortunately I am still stuck. One of the things that is consistently getting flagged in these scripts is: “-” . Any other ways to deal with that?
tell application "Adobe Illustrator"
set this_text to (every text frame of layer 1 of document 1)
set amended_text to (this_text's text 1 thru 2) & "-" & (this_text's characters 3 thru -3) & "-" & (this_text's characters -2 thru -1)
amended_text
return amended_text
end tell
And this is the error message I’m getting: Can’t get text 1 thru 2 of {text frame 1 of layer 1 of document 1 of application “Adobe Illustrator”}.
set this_text to (every text frame of layer 1 of document 1)
is returning a list of object references and you need to step through those and get the contents of each text frame. Try something like this:
set NewTextList to {}
tell application "Adobe Illustrator"
set TheTextFrames to (every text frame of layer 1 of document 1)
repeat with ATextFrame in TheTextFrames
set NewText to contents of ATextFrame
copy (NewText's text 1 thru 2) & "-" & (NewText's characters 3 thru -3) & "-" & (NewText's characters -2 thru -1) to end of NewTextList
end repeat
end tell
return NewTextList
I used a new number from our files - 108956HB, and ran this script:
set NewTextList to {}
tell application "Adobe Illustrator"
set TheTextFrames to (every text frame of layer 1 of document 1)
repeat with ATextFrame in TheTextFrames
set NewText to contents of ATextFrame
copy (NewText's text 1 thru 2) & "-" & (NewText's characters 3 thru -3) & "-" & (NewText's characters -2 thru -1) to end of NewTextList
end repeat
end tell
return NewText
return NewTextList
Nothing changed in Illustrator.
The results window says this: “108956HB”
The Event Log says this:
tell application "Adobe Illustrator"
get every text frame of layer 1 of document 1
{text frame 1 of layer 1 of document 1}
get contents of text frame 1 of layer 1 of document 1
"108956HB"
end tell
tell application "Adobe Illustrator"
activate
set TheTextFrames to (every text frame of layer 1 of document 1)
repeat with ATextFrame in TheTextFrames
set NewText to contents of ATextFrame
set contents of ATextFrame to (NewText's text 1 thru 2) & "-" & (NewText's characters 3 thru -3) & "-" & (NewText's characters -2 thru -1)
end repeat
end tell