AppleScript delimiters
How do I remove the last 3 text items?
My list is as follows:
{"Water", "Management", "in", "Ancient", "Greek", "Cities", "Jun", "1993", "pdf"}
I would like to remove the last 3 text item (e.g.: "Jun, "1993", and "pdf").
Here is my script so far:
set AppleScript's text item delimiters to ["USA."]
set stringToUse to "04.Oxford.University.Press.USA.Water.Mngt.in.Ancient.Greek.Cities.Jun.1993.pdf" as string
set stringUSA to last text item of stringToUse
set AppleScript's text item delimiters to ["."]
set pathNames to text item of stringUSA
return pathNames
As a general rule it's important that:
The number of text items in the list can be a variable
The text items to be deleted are always the last three
Try:
set AppleScript's text item delimiters to "USA."
set stringToUse to "04.Oxford.University.Press.USA.Water.Mngt.in.Ancient.Greek.Cities.Jun.1993.pdf" as string
set stringUSA to last text item of stringToUse
set AppleScript's text item delimiters to "."
set pathNames to text items 1 thru -4 of stringUSA
set AppleScript's text item delimiters to {""}
return pathNames
Related
Essentially, I am taking a column of data from an excel file and breaking it up into small groups. So:
10
20
30
40
50
60
etc...
broken up into:
"10, 20, 30, 40"
"50, 60, 70, 80"
etc
Using AppleScript, I assume you would nest loops, something along the lines of:
tell application "TextEdit"
set theText to text of front document as string
set myParas to every paragraph of theText
set myNum to the number of paragraphs of theText
repeat myNum times
repeat 4 times
end repeat
end repeat
end tell
I'm going to be updating data once a month that comes through as a column of numbers and text. I can strip out all the text easy enough, just would like to know the principle of how to break up or merge the paragraphs into smaller blocks.
For many complicated reasons, I am stuck with AppleScript and textEdit, so other alternates (such as massaging with javascript or textWrangler or whatever) is not an option.
Also, maybe textEdit can do this on it's own but the script I will be using will have lots of other operations based on the above result, so AppleScript has to do all the heavy lifting.
You can specify the step size in a repeat loop, so you could do something like:
tell application "TextEdit" to set theText to text of front document
set paras to paragraphs of theText
set step to 4 -- number of items in a group
repeat with i from 1 to (count paras) by step
try
buildString(items i thru (i + step - 1) of paras)
on error errmess number errnum -- index out of bounds
log errmess
if errnum is -128 then error number -128 -- quit
buildString(items i thru -1 of paras) -- just to the end
end try
end repeat
to buildString(someList)
set tempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set output to someList as text
set AppleScript's text item delimiters to tempTID
display dialog output -- show it
return output
end buildString
I want to clear some text from a textedit text but its only half working for me
tell application "TextEdit"
set documentText to text of document 1
set {oldTID, my text item delimiters} to {my text item delimiters, "Administration"}
set textBits to text items of documentText
set my text item delimiters to ""
set text of document 1 to textBits as text
set my text item delimiters to oldTID
set documentText to text of document 1
set {oldTID, my text item delimiters} to {my text item delimiters, "New Search"}
set textBits to text items of documentText
set my text item delimiters to ""
set text of document 1 to textBits as text
set my text item delimiters to oldTID
set documentText to text of document 1
set {oldTID, my text item delimiters} to {my text item delimiters, "Advanced"}
set textBits to text items of documentText
set my text item delimiters to ""
set text of document 1 to textBits as text
set my text item delimiters to oldTID
set documentText to text of document 1
set {oldTID, my text item delimiters} to {my text item delimiters, "Tips"}
set textBits to text items of documentText
set my text item delimiters to ""
set text of document 1 to textBits as text
set my text item delimiters to oldTID
end tell
For some reason "New Search" is still on the text document.
Is that possible otherwise to only keep the text from the line 2 to 3 for example?
Kind regards.
Multiple text item delimiter calls can be tricky for AppleScript. Try this:
tell application "TextEdit" to set documentText to text of document 1
tell application "TextEdit" to set text of document 1 to my RemoveThis(documentText, {"Administration", "New Search", "Advanced", "Tips"})
to RemoveThis(txt, termList)
repeat with eachTerm in termList
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, (eachTerm as text)}
set textBits to text items of txt
set AppleScript's text item delimiters to oldTID
set txt to (textBits as text)
end repeat
return txt
end RemoveThis
In this way, you can define a list of terms to be sifted through, and not worry about the coding details.
I have two Scrolling field and one containing some text and another containing some words. eg. (s1:s2) separated by colon my objective is to find how many words in the second Scrolling are appearing in first Scrolling field. (eg: if s1 is appearing in first scrolling field then it shows s1,1 it means s1 appearing one time). While executing my code it's work but if my first scrolling field is empty it shows some text also some times it takes some words in the first field. (eg:\documentclass)this word is not in 2'nd field.
I am using the following code.
global ar
global sam
on mouseUp
--set the caseSensitive to true
put 0 into tmp
put empty into sam
put the field SRText into myArrayT
split myArrayT by CR
put the number of lines of (the keys of myArrayT) into myArrayl
repeat for each key j in myArrayT
put myArrayT[j] into k
split k by colon
put k[1] into searchStr1
put the field "MytextField" into sss
repeat for each word ass in sss
if ass contains searchStr1 then
add 1 to tmp
put ass & CR & CR after sam
put sam into ar
end if
end repeat
end repeat
answer sam
answer tmp
--answer sa
end mouseUP
on mouseDown
answer ar
put ar into ss
get ss
repeat for each word tWord in it
add 1 to wordCount[tword]
end repeat
combine wordCount by return and comma
answer wordCount
--put empty into sam
end mouseDown
You really should use quotes around field names. I promise you'll realise some day, that you have been shooting yourself in the foot all the time.
Don't use the in front of object references,
field "Field Name"
button "Field name"
but do use the in front of property references:
the number of lines of fld "Field Name"
the text of fld "Field Name"
the name of this stack
There really use no reason for your obsession with arrays. Stop using arrays when you don't need them! It just makes things overly complicated and slow.
I have modifed your mouseUp handler. I didn't look at your mouseDown handler. I believe your mouseUp handler should look like this:
on mouseUp
--set the caseSensitive to true
put 0 into tmp
put empty into sam
repeat for each line k in fld "SRText"
set the itemDel to colon
put item 1 of k into searchStr1
repeat for each word ass in field "MytextField"
if ass contains searchStr1 then
add 1 to tmp
put ass & colon & tmp & cr after sam
end if
end repeat
put 0 into tmp
end repeat
put sam
end mouseUp
The script above is based on your approach and I really don't understand why you would do it this way and what you would do with the result. The script below is probably much more efficient and it seems more useful to me.
on mouseUp
// I assume fld SRText contains a list similar to
// "abc:1" & cr & "bcd:2" & cr & "cde:1" & cr & "def:4"
put fld "SRText" into myData
split myData by cr and colon
put the keys of myData into myData
// myData is not an array!
repeat for each word myWord in fld "MytextField"
if myWord is among the lines of myData then
// use an array to store data, not to loop through
if myCounts[myWord] is empty then
put 1 into myCounts[myWord]
else
add 1 to myCounts[myWord]
end if
end if
end repeat
combine myCounts by cr and colon
put myCounts
end mouseUp
I have a field "data" with Unicode text in it which displays properly. I want to copy a chunk of it and put it into another field called "someData".
I tried the following script in a button
on mouseUp
put word 2 of line 1 of the unicodeText of field "data" into t
set the unicodeText of field "someData" to t
end mouseUp
Non Unicode text displays fine in the field "someData" but Unicode text does not.
You could probably get away with UTF8 encoding then parsing then re-encoding
on mouseUp
put word 2 of line 1 of uniDecode(the unicodeText of field "data","UTF8") into t
set the unicodeText of field "someData" to uniEncode(t,"UTF8")
end mouseUp
on mouseUp
put unicode the unicodeText of word 2 of field "data" into field "someData"
end mouseUp
should work.
Marek
Here is another one-liner you can test:
set the unicodeText of field 2 to the unicodeText of word 2 of field 1
I am no expert in unicode, but there may be a clue in that LC treats most unicode stuff as properties. Because of this, one may set, say, the uniCodeText of a field:
set the unicodeText of fld 1 to "U+400"
But one cannot set that property, or any property, in a variable. Consider the following two handlers. It is assumed there exist two fields, "fld 1" and "fld 2".
on mouseUp
set the useUnicode to "true"
set the unicodeText of fld 1 to "U+400" -- an example
set the unicodeText of fld "f2" to the uniCodeText of fld 1
end mouseUp
on mouseUp
set the useUnicode to "true"
set the unicodeText of fld 1 to "U+400"
put fld 1 into temp
set the unicodeText of fld "f2" to temp
end mouseUp
The first works, the second does not. In your example, you try to put displayed uniCode into a variable. I don't think you can "put" that sort of thing. You have to set a property.
Now that said, check out the "put uniCode" command. This might get around the property thing. Write back if it does.
Craig Newman
I have some files that have some text, 50 newlines (don't ask), and then some more text. How can I get just the text after the newlines? Shell scripts are fine. Any answer would be appreciated, as I've been stuck on this for a while. Thanks!
set input to read POSIX file "/tmp/test" as «class utf8»
set d to ""
repeat 50 times
set d to d & linefeed
end repeat
set text item delimiters to d
text item 2 of input