sashaosonia

Q: 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

Close

Q: applescript find and pass variables with unique value to new string

  • All replies
  • Helpful answers

  • by Niel,Helpful

    Niel Niel Jul 4, 2016 3:22 PM in response to sashaosonia
    Level 10 (312,590 points)
    Mac OS X
    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)

  • by sashaosonia,

    sashaosonia sashaosonia Jul 4, 2016 3:22 PM in response to Niel
    Level 1 (8 points)
    Mac OS X
    Jul 4, 2016 3:22 PM in response to Niel

    thanks! it works

  • by Niel,

    Niel Niel Jul 4, 2016 3:53 PM in response to sashaosonia
    Level 10 (312,590 points)
    Mac OS X
    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)

  • by sashaosonia,

    sashaosonia sashaosonia Jul 4, 2016 5:08 PM in response to Niel
    Level 1 (8 points)
    Mac OS X
    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 javascript JS

      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's text 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 (count items of rgb_col) by 3

      set temp_list to {}

      if item this_item of rgb_col is not in temp_list then set temp_list to temp_list & item this_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 (count new_strings)

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

      set item this_list of new_strings to {item this_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 (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 rgb_col is not {} then set result_string to result_string & this_string

    end repeat

     

     

     

     

    result_string

     

     

  • by sashaosonia,

    sashaosonia sashaosonia Jul 4, 2016 5:10 PM in response to sashaosonia
    Level 1 (8 points)
    Mac OS X
    Jul 4, 2016 5:10 PM in response to sashaosonia

    111.png

  • by Niel,Solvedanswer

    Niel Niel Jul 4, 2016 5:24 PM in response to sashaosonia
    Level 10 (312,590 points)
    Mac OS X
    Jul 4, 2016 5:24 PM in response to sashaosonia

    Delete the 'exit repeat' line.

     

    (143292)

  • by sashaosonia,

    sashaosonia sashaosonia Jul 4, 2016 5:33 PM in response to Niel
    Level 1 (8 points)
    Mac OS X
    Jul 4, 2016 5:33 PM in response to Niel

    Delete the 'exit repeat' line.

     

    brevity is the sister of the talent

  • by alexanderPL,

    alexanderPL alexanderPL Jul 5, 2016 8:30 AM in response to Niel
    Level 1 (8 points)
    Mac OS X
    Jul 5, 2016 8:30 AM in response to Niel

    Niel, hello again. Is it possible of any optimization of the your script? Picture 200x250 counting at the macpro 8 hours and I do not knew how long it be. Photoshop pass data to script 6,5 hours ago.

  • by alexanderPL,

    alexanderPL alexanderPL Jul 5, 2016 8:33 AM in response to Niel
    Level 1 (8 points)
    Mac OS X
    Jul 5, 2016 8:33 AM in response to Niel

    Niel, hello again. Is it possible of any optimization of the your script? Picture 200x250 counting at the macpro 8 hours and I do not knew how long it be. Photoshop pass data to script 6,5 hours ago.