Applescript how to get part of the text and set it as variable?

Hello! I'm very newbie for Applescript. Please help.

 

Example.

display

dialog "" default answer "" buttons {"OK"}

default button 1

-->>Result

is something like {button returned:"OK", text

returned:"AAABBBCCCFFF111BBB222AAADDR"}

 

How

to get from result only part of the text (for example) "AAA" and

"BBB" and set both as variable?

Script

has to find "AAA" and set it as variable1

than

find BBB and set it to variable2

than

again search for the next "AAA" and set variable3

MacBook Pro 15", 10.12

Posted on Feb 10, 2019 10:49 AM

Reply
3 replies

Feb 10, 2019 3:19 PM in response to Platina Member

The following AppleScript, using a specialized Ruby handler will extract all of the same repeat character sequences from the input string — into an AppleScript list.


For this input:


and based on the code below, it will create an AppleScript list as follows for the search character "A":


charList = {"AAA", "AAA"}


and you set a variable:


set var1 to first item of charList


If you want all occurrences of "BBB", you set aChar to "B", and so forth.


set charList to {}

set theText to text returned of (display dialog "Get string" default answer "")
set aChar to "A"
set args to theText's quoted form & space & aChar's quoted form
set charList to paragraphs of my get_string(args)
log (items of charList)
return

on get_string(args)
	return do shell script "ruby <<-EOF - " & args & "
#!/usr/bin/ruby

workStr, theChar = ARGV
extract_array = workStr.split(/([\"#{theChar}\"]+)/).delete_if(&:empty?)
					   .reject { |x| !x.include?(\"#{theChar}\") }
puts extract_array if extract_array
EOF"
end get_string


Feb 11, 2019 5:04 AM in response to VikingOSX

The only difference between this posted code and the preceding is replacing "reject" and the not (!) operator in the Ruby code with the improved "select" operator. This is the equivalent of removing double negatives from an English sentence, though the result of the Ruby code is the same as previous.


set charList to {}

set theText to text returned of (display dialog "Get string" default answer "")
set aChar to "A"
set args to theText's quoted form & space & aChar's quoted form
set charList to paragraphs of my get_string(args)
log (items of charList)
return

on get_string(args)
	return do shell script "ruby <<-EOF - " & args & "
#!/usr/bin/ruby

workStr, theChar = ARGV
extract_array = workStr.split(/([\"#{theChar}\"]+)/).delete_if(&:empty?)
					   .select { |x| x.include?(\"#{theChar}\") }
puts extract_array if extract_array
EOF"
end get_string



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript how to get part of the text and set it as variable?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.