Just getting started scripting. In textwrangler line 1 says:
Chicago Cubs @ Atlanta Braves
I want the script to take everything in line 1 until the “@” and copy it.
Then take everything after the “@.”
The problem is it won’t always be those words. And it won’t always be the same amount of words before and after the “@.”
If I can get it copied I can get it after that. But it’s getting those words to the clipboard I can’t figure out.
Thanks so much!!
Maybe these instructions may help.
repeat with aVal in {"Chicago Cubs @ Atlanta Braves", "Chicago Kubs@ Atlanta Graves", "Chicago Qubs @Atlanta Bravos", "Chicago Clubs@Atlanta Gravos"}
set the clipboard to aVal
set {theBeg, theEnd} to my splitClipBoard()
log {theBeg, theEnd}
end repeat
on splitClipBoard()
set {the_Beg, the_End} to my decoupe(the clipboard as text, {" @ ", " @", "@ ", "@"})
return {the_Beg, the_End}
end splitClipBoard
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
Yvan KOENIG (VALLAURIS, France) lundi 29 décembre 2014 22:57:14
I may (probably was) a little confusing in what I meant.
-
I want what is before the @ copied and pasted. Then what is after the @ copied and pasted.
-
What does the first section of script do? Is it to insure against typo mistakes? Just trying to learn.
-
I’m wanting this script to be usable for other documents that would include very different lines on line 1, but all would include the @ symbol. Thus I need a script that functions on line 1 specifically not limited to any particular set of words.
Hi micahgentle. Welcome to MacScripter.
One problem with your question is that you don’t say what you want to do with each piece of text once it’s in the clipboard. The first piece obviously has to be dealt with before the second’s copied. Also, potential helpers don’t want to find out later that what you want to do is something which can be achieved more easily without the clipboard. Also (in this case), TextWrangler’s AppleScript and “vanilla” AppleScript have different ways of achieving the same thing. Knowing the texts’ ultimate destination(s) would help with the choice of which method to use.
Another problem is that your demo text has spaces immediately before and after the “@” symbol. Do you want these copied to the clipboard too or not? Yvan’s script assumes you don’t and removes them if they’re there.
Anyway, ignoring the spaces for now, and to get you started, the following shows both how to get the texts to the clipboard and how to store them in variables using TextWrangler’s scripting. (Two caveats here: I’m not able to use my Yosemite machine at the moment, so this has only been tested with TextWrangler 3.5.3 in Mac OS 10.5.8. And ‘characterOffset of found object of findResult’ is the offset in the entire document, so it only gives the desired result here after a search of line 1.)
tell application "TextWrangler"
set findResult to (find "@" searching in line 1 of text of text window 1)
if (found of findResult) then
set o to characterOffset of found object of findResult
-- Copy the first piece of text to the clipboard.
copy (characters 1 thru (o - 1) of line 1 of text of text window 1)
-- Do something here with the text in the clipboard.
-- Copy the second piece of text to the clipboard.
copy (characters (o + 1) thru -1 of line 1 of text of text window 1)
-- Do something here with the text in the clipboard.
-- Alternatively, store the text in variables:
set preAt to (characters 1 thru (o - 1) of line 1 of text of text window 1) as text
set postAt to (characters (o + 1) thru -1 of line 1 of text of text window 1) as text
else
display dialog "There's no "@" in the first line of this document!"
end if
end tell
This shows the same things using one of the possible vanilla AppleScript methods:
tell application "TextWrangler"
set line1 to (line 1 of text of text window 1) as text
end tell
set o to (offset of "@" in line1)
if (o > 0) then
-- Copy the first piece of text to the clipboard.
set the clipboard to text 1 thru (o - 1) of line1
-- Do something here with the text in the clipboard.
-- Copy the second piece of text to the clipboard.
set the clipboard to text (o + 1) thru -1 of line1
-- Do something here with the text in the clipboard.
-- Alternatively, store the text in variables:
set preAt to text 1 thru (o - 1) of line1
set postAt to text (o + 1) thru -1 of line1
else
display dialog "There's no "@" in the first line of this document!"
end if
Hey Micah,
I think you’re still not entirely clear.
Do you want to replace line 1 with the text already in it BUT exclude the “@”? (If not then where precisely are you wanting to put the text?)
TextWrangler makes this kind of thing very easy, because you can find/replace text directly - and you can find/replace within a string variable.
# Replacing the text directly.
tell application "TextWrangler"
tell front text window's line 1's text
replace " *@ *" using " " options {search mode:grep, case sensitive:false}
end tell
end tell
# Getting the text, replacing within the string variable, putting the processed text back.
tell application "TextWrangler"
tell text of front text window to set _text to contents of line 1
set _text to replace " *@ *" using " " searchingString _text options {search mode:grep}
tell text of front text window
set text of line 1 to _text
select insertion point before line 1
end tell
end tell