Applescript-set variable to item _ of variable

Discussion in 'Mac Programming' started by Cojust, Jan 27, 2011.

  1. Cojust macrumors newbie

    Joined:
    Jan 27, 2011
    #1
    Little help here, i use text items quite a fair bit, however, i have a slight problem. If i have a set amount of text items a variable will contain, for example, 3, this would work fine
    Code:
    set {var1,var2,var3} to text items of variable
    
    However, if i do not know exactly how many variables, eg just 2 text items are in the variable, it throws an error. What i use now to solve that is
    Code:
    try
    set var1 to text item 1 of variable
    on error
    set var1 to ""
    end
    
    Is there a more efficient way to do it?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You can try something like this:
    Code:
    if (count of text items of variable) ≥ 1 then
        set var1 to text item 1 of variable
    else
        set var1 to missing value
    end if
     
  3. Cojust thread starter macrumors newbie

    Joined:
    Jan 27, 2011
    #3
    My bad. What i wanted to do is as
    Code:
    try
    set var1 to text item 1 of variable
    on error
    set var1 to ""
    end
    
    , but more efficiently(if there is such a method)
     

Share This Page