Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can this bit of applescript be made simpler?

Although this is part of the script I am trying to make the script thinner so it makes sense later on.

There are a lot more lines to be added, can I make this simpler?

Many Thanks

Matt

set BootsSet to {"Boots", 1196, 1300}

set DPSet to {"DP", 1000, 1360}

set GlueSet to {"Glue", 1020, 1530}

set RevivalSet to {"Revival", 1500, 1500}

set WeSet to {"WE", 1500, 1500}

--and so on....



if brandName is in {"Boots"} or brandID is in {"BO22"} then

set brandgroup to BootsSet

else if brandName is in {"DP"} or brandID is in {"DJ30", "RA30", "DV11", "DJ11"} then

set brandgroup to DPSet

else if brandName is in {"Glue"} or brandID is in {"GW59"} then

set brandgroup to GlueSet

else if brandName is in {"Revivial"} or brandID is in {"RV47"} then

set brandgroup to RevivialSet

else if brandName is in {"WE"} or brandID is in {"WE57"} then

set brandgroup to WeSet

--and so on...

end if

Mac Pro, OS X Mavericks (10.9)

Posted on Mar 20, 2015 9:30 AM

Reply
Question marked as Best reply

Posted on Mar 20, 2015 9:57 AM

Hello


I'd use unnamed collection instead of named variables. Something like this:


set brandTable to {¬ {brandName:{"Boots"}, brandID:{"BO22"}, brandGroup:{"Boots", 1196, 1300}}, ¬ {brandName:{"DP"}, brandID:{"DJ30", "RA30", "DV11", "DJ11"}, brandGroup:{"DP", 1000, 1360}}, ¬ {brandName:{"Glue"}, brandID:{"GW59"}, brandGroup:{"Glue", 1020, 1530}}, ¬ {brandName:{"Revivial"}, brandID:{"RV47"}, brandGroup:{"Revival", 1500, 1500}}, ¬ {brandName:{"WE"}, brandID:{"WE57"}, brandGroup:{"WE", 1500, 1500}} ¬ } set brandName to "" set brandID to "DV11" set brandGroup to missing value repeat with b in brandTable set b to b's contents if brandName is in b's brandName or brandID is in b's brandID then set brandGroup to b's brandGroup exit repeat end if end repeat return brandGroup



Good luck,

H

10 replies
Question marked as Best reply

Mar 20, 2015 9:57 AM in response to MattJayC

Hello


I'd use unnamed collection instead of named variables. Something like this:


set brandTable to {¬ {brandName:{"Boots"}, brandID:{"BO22"}, brandGroup:{"Boots", 1196, 1300}}, ¬ {brandName:{"DP"}, brandID:{"DJ30", "RA30", "DV11", "DJ11"}, brandGroup:{"DP", 1000, 1360}}, ¬ {brandName:{"Glue"}, brandID:{"GW59"}, brandGroup:{"Glue", 1020, 1530}}, ¬ {brandName:{"Revivial"}, brandID:{"RV47"}, brandGroup:{"Revival", 1500, 1500}}, ¬ {brandName:{"WE"}, brandID:{"WE57"}, brandGroup:{"WE", 1500, 1500}} ¬ } set brandName to "" set brandID to "DV11" set brandGroup to missing value repeat with b in brandTable set b to b's contents if brandName is in b's brandName or brandID is in b's brandID then set brandGroup to b's brandGroup exit repeat end if end repeat return brandGroup



Good luck,

H

Mar 23, 2015 6:21 AM in response to Hiroto

Excellent Thanks.


I'm just trying to work out how I might use it in my script.


The next line would be to choose the brand,

set brandNames to choose from list {"Boots", "DP", "Glue", "Revival"} with prompt "Choose brands to send out."

How can I set this to the list to be each of the first items of brandGroup? and then set the answer "brandNames" to the brandGroup:{"Boots", 1196, 1300}} or which ever is selected?



One other question why in your script have you ' set brandID to "DV11" '

set brandName to "" set brandID to "DV11" set brandGroup to missing value



Many Thanks


Matt

Mar 23, 2015 6:46 AM in response to Hiroto

Just revising what I was trying to ask.

This I hope looks clearer,

Matt


tell application id "com.adobe.photoshop"

set textName to name of current document


set the1stLetter to the first character of textName --> the 1st letter

set BrandID to characters 1 thru 4 of textName as text


logBrandID


if the1stLetter is in {"5", "U"} or BrandID is in {"MJC_", "SAH_", "ARK_"} then

set brandNames to choose from list {brandGroup} with prompt "Choose brands to send out."


-- choose fromm list will be the first text item of the contents to brandGroup


--set brandNames to the brandGroup that was selected.

else

set brandNames to ""


-- set brandnames using the BrandID to find the brandGroup in the brandTable


end if


my photoshopProcess(brandGroup)


end tell

Mar 23, 2015 10:23 AM in response to MattJayC

Hello


If I understand it correctly, you may try something like this.



set textName to "MJC_123456" --set textName to "DJ30123456" set the1stLetter to textName's character 1 set brandID to textName's text 1 thru 4 if the1stLetter is in {"5", "U"} or brandID is in {"MJC_", "SAH_", "ARK_"} then set brandNames to choose from list {"Boots", "DP", "Glue", "Revival"} with prompt "Choose brands to send out." if brandNames = false then error number -128 -- user cancel set brandGroup to get_brandGroup(brandNames's item 1, missing value) else set brandGroup to get_brandGroup(missing value, brandID) end if return brandGroup on get_brandGroup(brandName, brandID) (* string brandName : brand name string brandID : brand id return list : brandGroup corresponding to given brandName or brandID in brandTable *) set brandTable to {¬ {brandName:{"Boots"}, brandID:{"BO22"}, brandGroup:{"Boots", 1196, 1300}}, ¬ {brandName:{"DP"}, brandID:{"DJ30", "RA30", "DV11", "DJ11"}, brandGroup:{"DP", 1000, 1360}}, ¬ {brandName:{"Glue"}, brandID:{"GW59"}, brandGroup:{"Glue", 1020, 1530}}, ¬ {brandName:{"Revival"}, brandID:{"RV47"}, brandGroup:{"Revival", 1500, 1500}}, ¬ {brandName:{"WE"}, brandID:{"WE57"}, brandGroup:{"WE", 1500, 1500}} ¬ } set brandGroup to missing value repeat with b in brandTable set b to b's contents if brandName is in b's brandName or brandID is in b's brandID then set brandGroup to b's brandGroup exit repeat end if end repeat return brandGroup end get_brandGroup




As for my setting brandID to "DV11" in my previous code, it is mere example to show how script works just as the textName in the script above.


Regards,

H

Mar 24, 2015 2:16 AM in response to Hiroto

That gives me the result I was looking for. I was wondering though is it possible to for the choose from list from the Brandtable, First item of the contents of brand group?


The only reason I was trying to do that it is so that as I add more to the list it would update the "choose from list" to reduce updating the script. Not sure if that is possible.

Again. Many thanks for you help.


Matt

Mar 24, 2015 4:32 AM in response to MattJayC

Hello


It is simple. In order to do it, we need to have brandTable in shared context not inside a handler. Something like this.



set brandTable to {¬ {brandName:{"Boots"}, brandID:{"BO22"}, brandGroup:{"Boots", 1196, 1300}}, ¬ {brandName:{"DP"}, brandID:{"DJ30", "RA30", "DV11", "DJ11"}, brandGroup:{"DP", 1000, 1360}}, ¬ {brandName:{"Glue"}, brandID:{"GW59"}, brandGroup:{"Glue", 1020, 1530}}, ¬ {brandName:{"Revival"}, brandID:{"RV47"}, brandGroup:{"Revival", 1500, 1500}}, ¬ {brandName:{"WE"}, brandID:{"WE57"}, brandGroup:{"WE", 1500, 1500}} ¬ } set textName to "MJC_123456" --set textName to "DJ30123456" set the1stLetter to textName's character 1 set brandID to textName's text 1 thru 4 if the1stLetter is in {"5", "U"} or brandID is in {"MJC_", "SAH_", "ARK_"} then set brandNames to choose from list my get_brandNames(brandTable) with prompt "Choose brands to send out." if brandNames = false then error number -128 -- user cancel set brandGroup to my get_brandGroup(brandTable, brandNames's item 1, missing value) else set brandGroup to my get_brandGroup(brandTable, missing value, brandID) end if return brandGroup on get_brandGroup(brandTable, brandName, brandID) (* list brandTable : list of records string brandName : brand name string brandID : brand id return list : brandGroup corresponding to given brandName or brandID in brandTable *) set brandGroup to missing value repeat with b in brandTable set b to b's contents if brandName is in b's brandName or brandID is in b's brandID then set brandGroup to b's brandGroup exit repeat end if end repeat return brandGroup end get_brandGroup on get_brandNames(brandTable) (* list brandTable : list of records return list : list of every brandName in brandTable *) set brandNames to {} repeat with b in brandTable set brandNames's end to b's brandName end repeat return brandNames end get_brandNames



Good luck,

H

Mar 24, 2015 8:52 AM in response to MattJayC

Hello


If you wish, you can eliminate brandName from record and use brandGroup's item 1 instead. Something like this.



set brandTable to {¬ {brandID:{"BO22"}, brandGroup:{"Boots", 1196, 1300}}, ¬ {brandID:{"DJ30", "RA30", "DV11", "DJ11"}, brandGroup:{"DP", 1000, 1360}}, ¬ {brandID:{"GW59"}, brandGroup:{"Glue", 1020, 1530}}, ¬ {brandID:{"RV47"}, brandGroup:{"Revival", 1500, 1500}}, ¬ {brandID:{"WE57"}, brandGroup:{"WE", 1500, 1500}} ¬ } set textName to "MJC_123456" --set textName to "DJ30123456" set the1stLetter to textName's character 1 set brandID to textName's text 1 thru 4 if the1stLetter is in {"5", "U"} or brandID is in {"MJC_", "SAH_", "ARK_"} then set brandNames to choose from list my get_brandNames(brandTable) with prompt "Choose brands to send out." if brandNames = false then error number -128 -- user cancel set brandGroup to my get_brandGroup(brandTable, brandNames's item 1, missing value) else set brandGroup to my get_brandGroup(brandTable, missing value, brandID) end if return brandGroup on get_brandGroup(brandTable, brandName, brandID) (* list brandTable : list of records string brandName : brand name string brandID : brand id return list : brandGroup corresponding to given brandName or brandID in brandTable * brandGroup = {brandName, width, height} *) set brandGroup to missing value repeat with b in brandTable set b to b's contents if brandName is in b's brandGroup or brandID is in b's brandID then set brandGroup to b's brandGroup exit repeat end if end repeat return brandGroup end get_brandGroup on get_brandNames(brandTable) (* list brandTable : list of records return list : list of every brandName (brandGroup's item 1) in brandTable * brandGroup = {brandName, width, height} *) set brandNames to {} repeat with b in brandTable set brandNames's end to b's brandGroup's item 1 end repeat return brandNames end get_brandNames



Regards,

H

Mar 24, 2015 9:02 AM in response to MattJayC

Or even something like this. All depends upon how to define each record in brandTable.



set brandTable to {¬ {brandName:{"Boots"}, brandID:{"BO22"}, brandSize:{1196, 1300}}, ¬ {brandName:{"DP"}, brandID:{"DJ30", "RA30", "DV11", "DJ11"}, brandSize:{1000, 1360}}, ¬ {brandName:{"Glue"}, brandID:{"GW59"}, brandSize:{1020, 1530}}, ¬ {brandName:{"Revival"}, brandID:{"RV47"}, brandSize:{1500, 1500}}, ¬ {brandName:{"WE"}, brandID:{"WE57"}, brandSize:{1500, 1500}} ¬ } set textName to "MJC_123456" --set textName to "DJ30123456" set the1stLetter to textName's character 1 set brandID to textName's text 1 thru 4 if the1stLetter is in {"5", "U"} or brandID is in {"MJC_", "SAH_", "ARK_"} then set brandNames to choose from list my get_brandNames(brandTable) with prompt "Choose brands to send out." if brandNames = false then error number -128 -- user cancel set brandGroup to my get_brandGroup(brandTable, brandNames's item 1, missing value) else set brandGroup to my get_brandGroup(brandTable, missing value, brandID) end if return brandGroup on get_brandGroup(brandTable, brandName, brandID) (* list brandTable : list of records string brandName : brand name string brandID : brand id return list : brandGroup corresponding to given brandName or brandID in brandTable * brandGroup = {brandName, width, height} where {width, height} = brandSize *) set brandGroup to missing value repeat with b in brandTable set b to b's contents if brandName is in b's brandName or brandID is in b's brandID then set brandGroup to {} & b's brandName & b's brandSize exit repeat end if end repeat return brandGroup end get_brandGroup on get_brandNames(brandTable) (* list brandTable : list of records return list : list of every brandName in brandTable *) set brandNames to {} repeat with b in brandTable set brandNames's end to b's brandName end repeat return brandNames end get_brandNames



Cheers,

H

Can this bit of applescript be made simpler?

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