Applescript using if then statements and choose from list - applescript

I'm working on a course selection script and each person should only be able to select three courses. I am having issues figuring out how to repeat choose from list if the user selects less than or more than three courses, and only proceed if three choices are selected. Converting the choices from the list to string should work however nothing happens after choosing from list
set theClassList to {"Math ", "English ", "Science ", "I&S ", "Design "}
repeat
set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed
set theClassString to theClass as string
if words in theClassString ≠ 3 then
display dialog "Please select three courses"
exit repeat
else if words in theClassString = 3 then
display dialog "Ok"
end if
end repeat

It's a clever idea to count the words in the theClassString (which would be done using number of words in theClassString, instead of simply words in theClassString). This would work the majority of the time, until a user includes "I&S" as one of their options, which sadly counts as two words, "I" and "S", since the ampersand is not a word character.
You also had your exit repeat in the wrong half of the if...then...else block, since you want to break the loop when the user selects 3 courses, not when they fail to select 3 courses.
Rather than attempting to coerce the result of the list selection into a string, you ought to just count the number of items in the result, which can be done in one of three ways:
count theClass
length of theClass
number in theClass
Here's a reworked version of your script:
property theClassList : {"Math ", "English ", "Science ", "I&S ", "Design "}
property text item delimiters : linefeed
set choices to missing value
repeat
if choices ≠ missing value then display dialog ¬
"You must select precisely three courses"
set choices to choose from list theClassList with prompt ¬
"Select three courses" with multiple selections allowed
if choices = false or the number of choices = 3 then exit repeat
end repeat
if choices = false then return
display dialog choices as text
...And here's a version that uses a recursive handler instead of a repeat loop:
property theClassList : {"Math", "English", "Science", "I&S", "Design"}
property text item delimiters : linefeed
to choose()
tell (choose from list theClassList ¬
with prompt ("Select three courses") ¬
with multiple selections allowed) to ¬
if it = false or its length = 3 then ¬
return it
display dialog "You must select precisely three courses"
choose()
end choose
display alert (choose() as text)

to make it easier by respecting the fluidity of your script. It would be better to declare your "theClass" as a rather string list and declare n as count of list. Below is your modified script or the following that takes your own but declaring n to count of words in "theClassString".`
set theClassList to {"Math", "English", "Science", "I & S", "Design"}
repeat
set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed
set theClassString to theClass as list
set n to count of theClassString
set n to do shell script "echo" & n
if n ≠ "3" then
display dialog "Please select three courses"
exit repeat
else if n = "3" then
display dialog "Ok"
end if
end repeat
Below
By declaring String
set theClassList to {"Math ", "English ", "Science ", "I&S ", "Design "}
repeat
set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed
set theClassString to theClass as string
set n to count of words in theClassString
set n to do shell script "echo " & n
if n ≠ "3" then
display dialog "Please select three courses"
exit repeat
else if n = "3" then
display dialog "Ok"
end if
end repeat

um maybe like this
set theClassList to {"Math ", "English ", "Science ", "I&S ", "Design "}
repeat
set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed
set theClassString to theClass as string
set n to count of words in theClassString
set n to do shell script "echo " & n
if n ≠ "3" then
display dialog "Please select three courses" buttons {"ok"}
else if n = "3" then
display dialog "Ok"
exit repeat
end if
end repeat

Related

Get text of selected field code in Microsoft Word using AppleScript

I'm making a Automator to jump from citation in Word to the reference software(Zotero). But I can't find a AppleScript to extract text of selected field code (the first step).
The field code in Word is
ADDIN ZOTERO_ITEM CSL_CITATION {"citationID":"AFUiwuqi","properties":{"formattedCitation":"[1]","plainCitation":"[1]","noteIndex":0},"citationItems":[{"id":9752,"uris":["http://zotero.org/users/6410528/items/YYTRWPHH"],"itemData":{"id":9752,"type":"article-journal","container-title":"Nature","DOI":"10.1038/s41586-019-1737-7","ISSN":"0028-0836, 1476-4687","issue":"7782","page":"324-329","title":"Controlled flight of a microrobot powered by soft artificial muscles","volume":"575","author":[{"family":"Chen","given":"Yufeng"},{"family":"Zhao","given":"Huichan"},{"family":"Mao","given":"Jie"},{"family":"Chirarattananon","given":"Pakpong"},{"family":"Helbling","given":"E. Farrell"},{"family":"Hyun","given":"Nak-seung Patrick"},{"family":"Clarke","given":"David R."},{"family":"Wood","given":"Robert J."}],"issued":{"date-parts":[["2019",11,14]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"}
Here is the script process:
Extract text from selected field code in Word (This is the question)
Get the uris text(http://zotero.org/users/6410528/items/YYTRWPHH)
Get the item-codes (YYTRWPHH).
Open url (zotero://select/library/items?itemKey=YYTRWPHH)
Now I use VBA to extract field code text, see below. But in this way, the file will be changed. So I want to do this via AppleScript.
Sub GetFiledsCodes()
Dim myRange As Range, myCodes As String
Set myRange = Selection.Range
With myRange
If .Fields.Count = 0 Then
MsgBox "No Code!", vbInformation
Exit Sub
Else
.Fields.Update
.TextRetrievalMode.IncludeFieldCodes = True
.TextRetrievalMode.IncludeHiddenText = True
myCodes = .Text
myCodes = VBA.Replace(myCodes, Chr(19), "{")
myCodes = VBA.Replace(myCodes, Chr(21), "}")
.SetRange .End, .End
.InsertAfter myCodes
.Font.Name = "Times New Roman"
.Font.Size = 12
.Cut
End If
End With
End Sub
PS:
Here is my process in Automator(it can work but using VBA):
Run AppleScript
on run {input, parameters}
tell application "Microsoft Word" to activate
tell application "Microsoft Word"
run VB macro macro name "GetFiledsCodes"
delay 0.5
end tell
return input
end run
Get contents from clipboard
Extract URLs from Text
Filter Paragraphs begin with http://zotero.org/users/
Copy to Clipboard
Run AppleScript
set myStr to do shell script "pbpaste"
tell application "Zotero" to activate
set AppleScript's text item delimiters to "
"
set myList to every text item of myStr
set zoterocode to ""
set codes to ""
repeat with j from 1 to the length of myList
set itemValue to item j of myList
set zoterocode to (do shell script "sed -E 's#http://zotero.org/users/[0-9]+/items/##g' <<< " & itemValue)
if j = 1 then
set codes to zoterocode
else
set codes to codes & "," & zoterocode
end if
end repeat
tell application "System Events"
key code 18 using {command down, control down, option down}
delay 0.5
set collectionKey to do shell script "pbpaste"
if collectionKey = myStr then
set theurl to "zotero://select/library/items?itemKey=" & codes
else
set theurl to collectionKey & "/items?itemKey=" & codes
end if
open location theurl
end tell
That helps a lot. Okay, so this isn't a turnkey solution for your question but I don't think you really need that as you'd probably end up having to tell me more about how this app works than is really necessary. So this script focuses on your initial question about getting the field codes/result ranges from a merge document.
I put together a simple mail merge consisting of labels and a data file with 8 records, each of which have 5 fields: {"«LastName»", "«JobTitle»", "«Company»", "«City»", "«Web»"}. The latter is the key field.
Basically, the script runs through the data merge document and cycles first through its fields, then the web field, and finally the web addresses.
Based on your script, I can't really determine what you are doing with each address so it finishes by collecting just the final part of each address in a list. The obscure parts for me are the pbpastes, the codes and the whole System Events block. This area would need tweaking.
Incidentally, it's quite likely that you can avoid some of the shell scripts but I can't say how yet. Obviously the script has some redundancies and could be further refined but I think it demonstrates how to extract the information you need. Take a look at it and let me know what issues there are that need addressing.
tell application "Microsoft Word"
set d1 to document "cardo_labels.docx"
set fContents to {} -- list of mergefield
set fResRange to {} -- list of result range, i.e. field merge data
repeat with x from 1 to (count of fields of d1)
set fcs to content of field code of field x of d1 --> " MERGEFIELD LastName "
set frr to content of result range of field x of d1 --> "Smith"
if fcs is not " NEXT " then -- ignore «Next Record»
set end of fContents to fcs
set end of fResRange to frr
end if
end repeat
--> single record example
fContents --> {" MERGEFIELD LastName ", " MERGEFIELD JobTitle ", " MERGEFIELD Company ", " MERGEFIELD City ", " MERGEFIELD Web "}
fResRange --> {"Smith", "President", "Acme Screw & Gear", "Metz", "http://zotero.org/users/1234/items/smith-metz"}
-- NB when not displaying 'merged data', fResRange will appear thusly: {"«LastName»", "«JobTitle»", "«Company»", "«City»", "«Web»"}
set webList to {}
repeat with y from 1 to (count of fResRange)
if item y of fResRange begins with "http://zotero.org/users/" then
set end of webList to (item y of fResRange)
end if
end repeat
--> {"http://zotero.org/users/1234/items/smith-metz"}
--> {"http://zotero.org/users/1234/items/smith-metz", "http://zotero.org/users/4222/items/branson-metz", "http://zotero.org/users/3236/items/house-metz", "http://zotero.org/users/3342/items/kurtz-london", "http://zotero.org/users/12345/items/jones-london"}
set urlPiece to {}
set AppleScript's text item delimiters to "/"
repeat with z in webList
set end of urlPiece to last text item of z
end repeat
-- contents of z
--> "http://zotero.org/users/1234/items/smith-metz"
set AppleScript's text item delimiters to ""
urlPiece
--> {"smith-metz"}
--> {"smith-metz", "jones-saopaolo", "branson-metz", "house-metz", "kurtz-london", "jones-london"}
end tell
Thanks to ideas from #Mockman.
Combining with the selection, here is the way to extract text from selected field code via AppleScript:
tell application "Microsoft Word"
tell selection
set fcs to content of field code of field of text object
end tell
end tell
fcs

Apple Script: Go back-function in menus

I'm not too versed in AppleScript, so maybe someone can help here:
I have several menus. Let's say, Menu A has three options, these three options lead to Menu B1, B2, B3, depending what options was chosen in Menu A. The B-menus lead to C-menus and so on. I am a bit bothered, that every time I "Cancel", I need to start from scratch. A "Go back"-button would be highly useful in this scenario.
I know that the choose from list-function does only allow Yes and no, so to say, but is it possible to somehow implement a back button?
My code looked like this (we're in Menu B):
if MenuB is false then error number -128 -- user canceled
Then I tried implementing the "Go back" function via:
if MenuB is false then set MenuA to (choose from list {"MenuB1", "MenuB2", "MenuB3"} with prompt "Menu A" default items "None" OK button name {"Go"} cancel button name {"Quit"})
However that creates a new menu and doesn't refer to the menu created before the Menu B script.
Tl;dr: So is it somehow possible to reference to existing menus based on choose from list without creating a new menu. Or is there a way to implement a "back"-button in menus via AppleScript?
Looking forward to your thoughts!
Code excerpt:
# Main Menu
set MainMenu to (choose from list {"Item1", "Item2", "Item3"} with prompt "Main Menu" default items "None" OK button name {"Go"} cancel button name {"Quit"})
if MainMenu is false then error number -128 -- user canceled
# Button 1: Item 1
if MainMenu contains "Item1" then
# Sub-Button 1: Item 1
set SubButton to (choose from list {"Create", "Rename"} with prompt "Settings" default items "None" OK button name {"Choose"} cancel button name {"Quit"})
if SubButton is false then error number -128 -- user canceled
# Sub-Sub-Button 1: Item 1
if SubButton contains "Create" then
(...)
Without knowing the exact number of menus and how they interact, the easiest way would probably be to implement a stack, where items can be pushed onto or popped from the stack depending on the dialog results. Then it is just a matter of organizing and connecting the menus, as it is doubtful you will run out of stack space before running out of patience. Trying to navigate back and forth across nested or interconnected menus by just using a long sequence of if statements leads to the dark side.
To make the menu lists a little more manageable, they can be put into a collection object such as a record, rather than hard coding the dialog statements, with the record keys being used to keep track of the current and previous lists. From each choose from list dialog, the current choice can be pushed onto the top of the stack, and the previous item popped from it if going back.
A little bit of AppleScriptObjC is used in the following example to dynamically look up the record keys, since regular AppleScript doesn’t have a way to do that. The lists contain the various menu items, which are also used to look up the keys for other menu lists - surrounding the record keys with pipes allows them to have spaces and punctuation, so pretty much any text can be used as a key. Using menu item text as record keys also allows circling and jumping around (the example script also demonstrates this), so just be aware when laying out the menu connections. The ultimate end values are kept in a different record, and are single items (string or list). The final result is also used in a choice dialog for verification and to allow going back.
Note that the funky comma placements are used to try to make the record formatting a little easier to read.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property stack : {"base"} -- LIFO, starting with key of the base menu
property menus : ¬
{base:{"menu A", "menu B", "menu C"} ¬
, |menu A|:{"subMenu A1", "subMenu A2", "subMenu A3"} ¬
, |menu B|:{"subMenu B1", "subMenu B2", "subMenu B3"} ¬
, |menu C|:{"A key", "A little longer key", "The quick brown fox jumped over the lazy dog's key"} ¬
, |subMenu A1|:{"Action1", "Action2", "Action3"} ¬
, |subMenu A2|:{"Action4", "Action5", "Action6"} ¬
, |subMenu A3|:{"Action7", "Action8", "Action9"} ¬
, |subMenu B1|:{"menu A", "menu B", "menu C"} ¬
, |subMenu B2|:{"subMenu A1", "subMenu A2", "subMenu A3"} ¬
, |subMenu B3|:{"A key", "A little longer key", "The quick brown fox jumped over the lazy dog's key"} ¬
, |A key|:{"Action1", "Action2", "Action3"} ¬
, |A little longer key|:{"Action4", "Action5", "Action6"} ¬
, |The quick brown fox jumped over the lazy dog's key|:{"Action7", "Action8", "Action9"}}
property actions : ¬
{Action1:"Action one", Action2:"Action two", Action3:"Action three", Action4:"Action four", Action5:"Action five", Action6:"Action six", Action7:"Action seven", Action8:"Action eight", Action9:"Action 9"}
on run -- example
set menuDict to current application's NSDictionary's dictionaryWithDictionary:menus
set actionDict to current application's NSDictionary's dictionaryWithDictionary:actions
set cancelName to "Quit"
repeat
set tos to first item of stack
set choices to (menuDict's objectForKey:tos) -- get menu
if choices is missing value then -- handle missing key
log "menu key " & quoted form of tos & " not found, trying actions"
set choices to (actionDict's objectForKey:tos) -- try actions
if choices is missing value then log "actions key " & quoted form of tos & " not found"
end if
set theChoice to (choose from list (choices as list) default items {"msng"} cancel button name cancelName) -- select missing value
if theChoice is false then
if (count stack) is 1 then error number -128 -- cancel
set stack to rest of stack -- pop
if (count stack) is 1 then set cancelName to "Quit" -- last one
else
if (count (choices as list)) is 1 then exit repeat -- final action result selected
set beginning of stack to theChoice as text -- push
set cancelName to "Go Back"
end if
end repeat
#showResult(stack, actionDict) -- uncomment to show the result
runWorkflow(((actionDict's objectForKey:(first item of stack)) as text))
end run
to showResult(stack, dictionary) -- show the final result action
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " > "}
set keys to (reverse of stack) as text
set AppleScript's text item delimiters to tempTID
set value to quoted form of ((dictionary's objectForKey:(first item of stack)) as text)
if value is quoted form of "missing value" then set value to value & return & return & "A menu or actions key was not found - see log."
display dialog "Key sequence: " & keys & return & "Final value: " & value with title "Final Result" buttons "OK" default button 1
end showResult
to runWorkflow(workflowName) -- run an Automator workflow
set basepath to POSIX path of (path to desktop) -- change as desired
set workflowpath to basepath & workflowName -- POSIX path/name to add to the basePath
set command to "/usr/bin/automator " & quoted form of workflowpath
set output to (do shell script command)
-- do whatever with the output
end runAction

Return results from multiple list choice in Applescript

This script is for use in Capture One where I am assigning people's names to the EXIF data.
I am trying to return the results of a list that could be one or more choices made by the user. I can get it to work with using item 1 in the list but I can't figure out how to deal with someone choosing 2 or more names from anywhere in the list?
Thanks for any help you can offer.
tell application "Capture One 11"
set peopleChoices to {"Abbie", "Charlie", "De-Arne", "Dean", "Jason", "Marlene", "Peta ", "Phoenix", "Rod", "Vanessa", "Yvonne"}
set peopleList to choose from list peopleChoices with prompt "Select your keyword/s:" with multiple selections allowed
if the result is not false then
set exif_keywords to item 1 of the result
end if
set selectedVariants to get selected variants
repeat with i from 1 to number of items in selectedVariants
set this_item to item i of selectedVariants
set theID to id of (parent image of this_item)
do shell script "/usr/local/bin/exiftool -Subject='" & exif_keywords & "' -m -overwrite_original_in_place " & quoted form of theID
reload metadata this_item
end repeat
display dialog "EXIF data has been updated"
end tell
You are constraining the list to one item in this line
set exif_keywords to item 1 of the result
Just change it to
set exif_keywords to result
I don't know how the keywords are supposed to be passed in the exiftool line, you might flatten the list with text item delimiters, this example joins the list comma separated. Replace "," with space if the parameters have to be space separated.
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
set exif_keywords to exif_keywords as text
set AppleScript's text item delimiters to ASTID
I have included the whole working script below in case anyone else is looking for something similar. The "-sep" is part of exiftool and splits the string depending on what you put after it. I had to escape it for the shell script line but it nromally does not have the back slashes.
tell application "Capture One 11"
set peopleChoices to {"Abbie", "Charlie", "De-Arne", "Dean", "Jason", "Marlene", "Peta", "Phoenix", "Rod", "Vanessa", "Yvonne"}
set peopleList to choose from list peopleChoices with prompt "Select your keyword:" with multiple selections allowed
if the result is not false then
set exif_keywords to result
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
set exif_keywords to exif_keywords as text
set AppleScript's text item delimiters to TID
end if
set selectedVariants to get selected variants
repeat with i from 1 to number of items in selectedVariants
set this_item to item i of selectedVariants
set theID to id of (parent image of this_item)
do shell script "/usr/local/bin/exiftool -sep \",\" -Keywords='" & exif_keywords & "' -m -overwrite_original_in_place " & quoted form of theID
reload metadata this_item
end repeat
display dialog "EXIF data has been updated"
end tell

Applescript Returned Text not Splitting

Ive been playing with applescript for about 2 weeks now, but I have hit a problem
Im trying to create an applescript that reads all the names of the folders on our server.
Then displays them in a drop down menu so that I can select the client.
The problem I have is that it is displaying the result as one selection option as a big sentence and is not separating each client, so they can be selected individually.
so far I have:
set theFolder to alias "server:"
tell application "Finder"
set theText to name of theFolder & return
set k to 0
repeat with thisSubfolder in (get folders of theFolder)
set k to k + 1
set theText to theText & name of thisSubfolder & return
end repeat
end tell
set l to {theText}
set l2 to ""
repeat with i in l
set l2 to l2 & quoted form of i & " "
end repeat
do shell script "/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog \\
standard-dropdown --title Title --text Text --items " & l2
set {button, answer} to paragraphs of result
if button is 1 then
return item {answer + 1} of l
end if
Many thanks
D
When you do this:
set l to {theText}
You're just creating a list of one item (your string), which means you end up with this:
{"theFolder
folder1
folder2
folder3
"}
You're then repeating in that "list," trying to add spaces between the items. But, you don't have a list really. You have a one item list, with return-delimited strings.
The best way to get a list of folder names would be to get them from System Events. Notice that in this case, you have to create a list with the name of the first folder as the only item. Otherwise, the & operation will join everything together as a string, instead of creating a list.
tell application "System Events"
set l to (name of theFolder as list) & name of folders of theFolder
end tell
There are also some syntactical issues that will hurt you later:
1 != "1"
CocoaDialog returns a string, with the button number: "1". You are using if button is 1. For equality, it should be if button is "1".
Parentheses are used for grouping, not brackets
If button is "1", you are returning item {answer + 1} of l. I blame Applescript for letting this work when it shouldn't. You're actually creating a list with a number, which then gets coerced by Applescript for the list index. Here are all the steps, assuming answer is 0:
item {answer + 1} of l gets turned into
item {1} of {folder1, folder2, folder3}
Applescript coerces to item 1 of {folder1, folder2, folder3}
Value returned: folder1
Here is a fully updated version of your script:
set theFolder to alias "server:"
tell application "System Events"
set l to {name of theFolder} & name of folders of theFolder
end tell
set args to ""
repeat with i from 1 to (count l)
set args to args & quoted form of item i of l
if i < (count l) then
set args to args & " "
end if
end repeat
do shell script "/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog \\
standard-dropdown --title Title --text Text --items " & args
set {button, answer} to paragraphs of result
if button is "1" then
return item (answer + 1) of l
end if
Change the line:
set l to {theText}
to:
set l to paragraphs of theText
and you should be good to go.

Setting the width of a variable in applescript

I am trying to write an apple script that sequentially names my TV shows with "S0XEYY" where YY is a two digit number. I have the following:
tell application "iTunes"
set p to selection
set c to count of p's items
set fixed indexing to true
repeat with i from 1 to c
set t to item i of p
try
set the episode number of t to i
set the episode ID of t to "S" & (text returned of a) & "E" & i
set the season number of t to text returned of a
end try
end repeat
However, I need the
(text returned of a)
as well as
i
to be of width 2, padded with a 0. How do I go about doing this?
Here is how I went about it:
if i ≤ 9 then
set the episode ID of t to episode ID of t & "0" & i
else
set the episode ID of t to episode ID of t & i
which I am sure is not the best way to do it, because it assumes no more then 2 characters are needed, but its the best solution I have, which works for this application because TV seasons never (that I have seen) exceed 20-30 episodes.

Resources