applescript find and pass variables with unique value to new string

hello again,

I need a solution to make this:


we have the string

{0, 0, "62 21 37", 1, 0, "62 21 37", 2, 0, "108 50 56", 3, 0, "167 102 95", 4, 0, "108 50 56", 5, 0, "145 84 83", 6, 0, "109 50 58", 7, 0, "109 50 58", 8, 0, "129 62 66", 9, 0, "120 56 62", 10, 0, "104 47 57"} (just example from result window)

this is a pixels data - x,y and RGB as a text

how can I separate this at the different strings organized by RGB, to create something like

{"109 50 58", 6,0 7,0 }

{"62 21 37", 0,0,1,0}

{"108 50 56", 2,0,4,0}

and etc.

thanks

MacBook Pro, OS X El Capitan (10.11.2)

Posted on Jul 4, 2016 2:21 PM

Reply
9 replies

Jul 4, 2016 3:22 PM in response to sashaosonia

Try using:


set the_string to {0, 0, "62 21 37", 1, 0, "62 21 37", 2, 0, "108 50 56", 3, 0, "167 102 95", 4, 0, "108 50 56", 5, 0, "145 84 83", 6, 0, "109 50 58", 7, 0, "109 50 58", 8, 0, "129 62 66", 9, 0, "120 56 62", 10, 0, "104 47 57"}

set new_strings to {}

repeat with this_item from 3 to (count items of the_string) by 3

set temp_list to {}

if item this_item of the_string is not in temp_list then set temp_list to temp_list & item this_item of the_string

repeat with this_list_item in items of temp_list

set new_strings to new_strings & (this_list_item as list)

end repeat

repeat with this_list from 1 to (count new_strings)

if (item this_list of new_strings as string) is item this_item of the_string then

set item this_list of new_strings to {item this_list of new_strings} & items (this_item - 1) thru (this_item - 2) of the_string

end if

end repeat

end repeat

repeat with this_list_1 from 1 to (count items of new_strings)

repeat with this_list_2 from (this_list_1 + 1) to (count items of new_strings)

if item this_list_1 of new_strings is not {} and item this_list_2 of new_strings is not {} then

if item 1 of item this_list_1 of new_strings is item 1 of item this_list_2 of new_strings then

set item this_list_1 of new_strings to item this_list_1 of new_strings & (items 2 thru 3 of (get item this_list_2 of new_strings))

set item this_list_2 of new_strings to {}

exit repeat

end if

end if

end repeat

end repeat

set result_string to {}

repeat with this_string in new_strings

if this_string is not {} then set result_string to result_string & this_string

end repeat


(143290)

Jul 4, 2016 3:53 PM in response to sashaosonia

To format the result as originally specified, add the following after the end of the existing script:


set output_list to the result

set final_string to {A:{}} as list

repeat ((count strings of output_list) - 1) times

set final_string to final_string & {{}}

end repeat

set destination_string to 0

repeat with the_result_item in output_list

if class of the_result_item is string then set destination_string to destination_string + 1

set item destination_string of final_string to item destination_string of final_string & the_result_item

end repeat

final_string


(143291)

Jul 4, 2016 5:08 PM in response to Niel

niel, your previous script not takes results bigger than four numbers at color and in final result takes repeating colors. can you say what needs to change?



on pix_val(x, y)

set JS to "sampleDoc();

function sampleDoc() {

var doc, sam, unts, x;

units = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

doc = app.activeDocument;

doc.colorSamplers.removeAll();

sam = doc.colorSamplers.add( [ " & x & ", " & y & " ] );

app.preferences.rulerUnits = units;

return parseInt(sam.color.rgb.red)+' '+parseInt(sam.color.rgb.green)+' '+parseInt(sam.color.rgb.blue);

};"


tell application "Adobe Photoshop CS4"


-- open your document here…

set col to do javascriptJS

return col


end tell

end pix_val





tell application "Adobe Photoshop CS4"

set y_max to height of current document

set x_max to width of current document

end tell


set yy to 0

set gt to ","

set val_list to {}

--set y_max to 5 -- вернуть потом значене к переменной

set rgb_col to {}

repeat y_max times

set xx to 0

repeat x_max times

set r_g_b to pix_val(xx, yy) --as text

set AppleScript'stext item delimiters to space

set rgb_col to rgb_col & xx & yy & r_g_b


--set val_list to val_list & {xx, yy, rgb_col}

set xx to xx + 1

end repeat

set yy to yy + 1

end repeat



set new_strings to {}

repeat with this_item from 3 to (countitems of rgb_col) by 3

set temp_list to {}

if itemthis_item of rgb_col is not in temp_list then set temp_list to temp_list & itemthis_item of rgb_col

repeat with this_list_item in items of temp_list

set new_strings to new_strings & (this_list_item as list)

end repeat

repeat with this_list from 1 to (countnew_strings)

if (itemthis_list of new_strings as string) is itemthis_item of rgb_col then

set itemthis_list of new_strings to {itemthis_list of new_strings} & items (this_item - 1) thru (this_item - 2) of rgb_col

end if

end repeat

end repeat

repeat with this_list_1 from 1 to (countitems of new_strings)

repeat with this_list_2 from (this_list_1 + 1) to (countitems of new_strings)

if itemthis_list_1 of new_strings is not {} and itemthis_list_2 of new_strings is not {} then

if item 1 of itemthis_list_1 of new_strings is item 1 of itemthis_list_2 of new_strings then

set itemthis_list_1 of new_strings to itemthis_list_1 of new_strings & (items 2 thru 3 of (get itemthis_list_2 of new_strings))

set itemthis_list_2 of new_strings to {}

exit repeat

end if

end if

end repeat

end repeat

set result_string to {}

repeat with this_string in new_strings

if rgb_col is not {} then set result_string to result_string & this_string

end repeat





result_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 find and pass variables with unique value to new string

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