Thursday, February 23, 2017

#26 2015-08-24 03:10:40 pm

Ignacio
Member
From: Santiago - Chile
Registered: 2010-04-23
Posts: 5

Re: Using AppleScript's Text Item Delimiters

In Applescript 2.4, some of the examples in the first script return something very different.
It´s seems that, even though I have the same TID "", AS recognizes "-" and "*" as a TID; and don't consider "_" as a word.

(If I ask for "length of Applescript's text item delimiters" the answer is: 1)

Applescript:

words of "Hi, I'm Peggy-Sue" --> {"Hi", "I'm", "Peggy-Sue"}
-- {"Hi", "I'm", "Peggy", "Sue"}

words of "Now*is the.time&for all_good (folks) to learn-AppleScript"
--> {"Now", "*", "is", "the.time", "&", "for", "all", "_", "good", "folks", "to", "learn-AppleScript"}
-- {"Now", "is", "the.time", "for", "all_good", "folks", "to", "learn", "AppleScript"}


words of "set-piece is hyphenated" --> {"set-piece", "is", "hyphenated"} - the hyphen isn't separated
-- {"set", "piece", "is", "hyphenated"}  (now it's not)

words of "funny*word with asterisk" --> {"funny", "*", "word", "with", "asterisk"} - the asterisk is separate
-- {"funny", "word", "with", "asterisk"}

Offline

 

#27 2015-10-29 04:35:47 pm

collin
Member
Registered: 2015-10-29
Posts: 2

Re: Using AppleScript's Text Item Delimiters

Hello, first of all, that tutorial is awesome, it gave me great new ideas for my application  (btw I'm creating a calculator).
I am referring to the first part of the tutorial, the "word of" part. For the "minus",  "times" and "divided by" section of the calculator I can't get the second word, which would be "-",  "*" or "/". Here's my code for the 4 basics.

Applescript:


set response to text returned of (display dialog "Enter one of the 4 basic arithmetic operations
               (+, -, *, /)"
default answer "" buttons {"Continue"} default button 1 with title "Basic arithmetic operations")
           set a1 to the result
           try
               repeat with aCharacter in response
                   if (aCharacter as text) is not in allowedAll then
                       display dialog "Please enter numbers only" buttons {"OK"} cancel button 1 default button 1
                   end if
                   if (word 2) of a1 is "+" then
                       set a2 to (word 1) of a1
                       set a3 to (word 3) of a1
                       set a4 to a2 + a3
                       display dialog a2 & " + " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
                       if the button returned of the result is "Okay" then
                           exit repeat
                       else
                           set the clipboard to a3 as text
                           exit repeat
                       end if
                   else if (word 2) of a1 is "-" then
                       set a2 to (word 1) of a1
                       set a3 to (word 3) of a1
                       set a4 to a2 - a3
                       display dialog a2 & " - " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
                       if the button returned of the result is "Okay" then
                           exit repeat
                       else
                           set the clipboard to a3 as text
                           exit repeat
                       end if
                   else if (word 2) of a1 is "*" then
                       set a2 to (word 1) of a1
                       set a3 to (word 3) of a1
                       set a4 to a2 * a3
                       display dialog a2 & " ・ " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
                       if the button returned of the result is "Okay" then
                           exit repeat
                       else
                           set the clipboard to a3 as text
                           exit repeat
                       end if
                   else if (word 2) of a1 is "/" then
                       set a2 to (word 1) of a1
                       set a3 to (word 3) of a1
                       set a4 to a2 / a3
                       display dialog a2 & " : " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
                       if the button returned of the result is "Okay" then
                           exit repeat
                       else
                           set the clipboard to a3 as text
                           exit repeat
                       end if
                   end if
               end repeat
               exit repeat
           end try

But as I said, that doesn't work with the "minus",  "times" and "divided by" section. So I tried this workaround, as described in the tutorial

Applescript:


set a1 to "4/2"
set x to AppleScript's text item delimiters
set AppleScript's text item delimiters to ASCII character 47 -- (that's the slash)
return word 2 of a1
set AppleScript's text item delimiters to x

but that still returnes 2, which is supposed to be the third word sad
Your help is greatly apprechiated big_smile

Last edited by collin (2015-10-29 04:46:45 pm)


Filed under: Math, ASCII, word of

Offline

 

#28 2015-10-29 04:51:06 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4654

Re: Using AppleScript's Text Item Delimiters

Glad you liked the tutorial, Colin. Your problem is that "/", "*", etc. are not words in AppleScript, they are characters.

Try this:

Applescript:

set theString to "a / c . e"
set W to words of theString --> "a", "c", "e"

as you see, only the letters (separated by spaces) show up. You'll have to hunt for those characters specifically.

collin wrote:

Hello, first of all, that tutorial is awesome, it gave me great new ideas for my application  (btw I'm creating a calculator).
I am referring to the first part of the tutorial, the "word of" part. For the "times" and "divided by" section of the calculator I can't get the second word, which would be "*" or "/". Here's my code for the 4 basics.

Applescript:


set response to text returned of (display dialog "Enter one of the 4 basic arithmetic operations
               (+, -, *, /)"
default answer "" buttons {"Continue"} default button 1 with title "Basic arithmetic operations")
           set a1 to the result
           try
               repeat with aCharacter in response
                   if (aCharacter as text) is not in allowedAll then
                       display dialog "Please enter numbers only" buttons {"OK"} cancel button 1 default button 1
                   end if
                   if (word 2) of a1 is "+" then
                       set a2 to (word 1) of a1
                       set a3 to (word 3) of a1
                       set a4 to a2 + a3
                       display dialog a2 & " + " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
                       if the button returned of the result is "Okay" then
                           exit repeat
                       else
                           set the clipboard to a3 as text
                           exit repeat
                       end if
                   else if (word 2) of a1 is "-" then
                       set a2 to (word 1) of a1
                       set a3 to (word 3) of a1
                       set a4 to a2 - a3
                       display dialog a2 & " - " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
                       if the button returned of the result is "Okay" then
                           exit repeat
                       else
                           set the clipboard to a3 as text
                           exit repeat
                       end if
                   else if (word 2) of a1 is "*" then
                       set a2 to (word 1) of a1
                       set a3 to (word 3) of a1
                       set a4 to a2 * a3
                       display dialog a2 & " ・ " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
                       if the button returned of the result is "Okay" then
                           exit repeat
                       else
                           set the clipboard to a3 as text
                           exit repeat
                       end if
                   else if (word 2) of a1 is "/" then
                       set a2 to (word 1) of a1
                       set a3 to (word 3) of a1
                       set a4 to a2 / a3
                       display dialog a2 & " : " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
                       if the button returned of the result is "Okay" then
                           exit repeat
                       else
                           set the clipboard to a3 as text
                           exit repeat
                       end if
                   end if
               end repeat
               exit repeat
           end try

But as I said, that doesn't work with the "times" and "divided by" section. So I tried this workaround, as described in the tutorial

Applescript:


set a1 to "4/2"
set x to AppleScript's text item delimiters
set AppleScript's text item delimiters to ASCII character 47 -- (that's the slash)
return word 2 of a1
set AppleScript's text item delimiters to x

but that still returnes 2, which is supposed to be the third word sad
Your help is greatly apprechiated big_smile


iMac running OS X 10.11.6

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.059 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)