Hey gang, it’s been a while since I got stumped but I’m trying some new practices here so am reaching out. Thanks in advance.
As the title suggests, I’m trying to reference a variable that I’ve built from data & strings.
Can anyone give me the magic key that will make the following script work because I feel like it should be able to be done?
property theSupplier : "Coles Supermarket"
set theSupplierList to {"Coles Supermarket", "Delica"}
set Def_ColesSupermarket to {thePaymentType:"BoM Mastercard", theAccount:"COS - Food", RecordID:"33"}
set Def_Delica to {thePaymentType:"Cash Drawer", theAccount:"COS - Food", RecordID:"493"}
set theSupplier to choose from list theSupplierList with title "Choose Supplier" default items {theSupplier}
set theSupplier to theSupplier as Unicode text
set z to ""
repeat with x in (characters of theSupplier)
set j to x as Unicode text
if j is not " " then
set z to z & x
end if
end repeat
set supplierDefault to "Def_" & z
set RecordID to RecordID of supplierDefault
return RecordID
variable names are evaluated at compile time, it’s not possible to do that at runtime (apart from second level evaluation which is not recommended).
This is an easier solution using a list of records and an index number, you can even type in the number to select the list item
property theSupplier : "Coles Supermarket"
set theSupplierList to {"1 Coles Supermarket", "2 Delica"}
set supplierDataRecords to {{thePaymentType:"BoM Mastercard", theAccount:"COS - Food", RecordID:"33"}, ¬
{thePaymentType:"Cash Drawer", theAccount:"COS - Food", RecordID:"493"}}
set theSupplier to choose from list theSupplierList with title "Choose Supplier" default items {theSupplier}
if theSupplier is false then return
set defIndex to word 1 of item 1 of theSupplier
set supplierDefault to item defIndex of supplierDataRecords
set RecordID to RecordID of supplierDefault
return RecordID
or if you don’t like the prefix index numbers, this is an alternative
property theSupplier : "Coles Supermarket"
property supplierDataRecords : {{name:"Coles Supermarket", thePaymentType:"BoM Mastercard", theAccount:"COS - Food", RecordID:"33"}, ¬
{name:"Delica", thePaymentType:"Cash Drawer", theAccount:"COS - Food", RecordID:"493"}}
set theSupplierList to {}
repeat with aRecord in supplierDataRecords
set end of theSupplierList to name of aRecord
end repeat
set theSupplier to choose from list theSupplierList with title "Choose Supplier" default items {theSupplier}
if theSupplier is not false then
set theSupplier to item 1 of theSupplier
repeat with i from 1 to count supplierDataRecords
tell item i of supplierDataRecords
if its name is theSupplier then return its RecordID
end tell
end repeat
end if
return missing value
StefanK, you the man! That solution is perfect and I love it because I’ll be able to use it in other parts of the script and in other apps. I’m really glad I decided to try to improve my scripting for this job and do things better.
Oh, also, Shane. I can’t believe you were the first to reply. It’s incredible that after nearly 10 years, we would cross paths again. I learned the fundamentals of AS from your very great self when I was looking to improve workflows at PMP Digital. Good times. I’m still using my applescripting knowledge and don’t think I could have done a lot of what I’ve done without it. I’m actually very grateful that you took the time with me all those years ago. Thank you. Hope all’s well with you.
Is it possible to load the supplier data from a text file (thereby making source data easier to edit)?
I’m using the name method over the the numbering.
Here’s what I have so far:
tell application "Finder"
set HomeSpot to (path to desktop) as Unicode text
set x to HomeSpot & "aaaSuppliers.txt"
set FileContents to paragraphs of (read file x) as list
--return FileContents
set CurrentTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\""
set theSupplierList to {}
repeat with aRecord in FileContents
set end of theSupplierList to text item 2 of aRecord
end repeat
set theSupplier to choose from list theSupplierList with title "Choose Supplier" --default items {theSupplier}
if theSupplier is not false then
set theSupplier to item 1 of theSupplier
repeat with i from 1 to count FileContents
tell item i of FileContents
if it's name is theSupplier then return its RecordID
end tell
end repeat
end if
set AppleScript's text item delimiters to CurrentTID
end tell
I think this has been covered before, but I think you can create a variable from a string. But, you can’t use the variable or something like that. Ive forgotten how it went, but I think it was with ‘run script’. In the end I think it wasn’t worth it and was better to write a better script.
either you write a just text file to disk, then you can’t read it as list, because there is no AppleScript list information in the file, or you write the property supplierDataRecords as (AppleScript) list, then you can access the list of records. The text file contains tokens to indicate the list, record and key information
Yeah, it’s coming back to me now. You create the variable from the string and place it in a list or was it a record. Anyway, you can create an undefined variable like this:
run script "MyUndefinedVariable"
Edited: think I’m on to something. But, maybe not. Darn, forgot what I was thinking about. Damn, almost had something. Thinking I’m beating a dead horse or however that expression goes. I must have gone through this a thousand times before.
The best way was to enter your strings that you want as variables into the list. Then you access those strings buy using ‘item 1’, ‘item 2’, etc… There was no direct solution to this.