Applescript, converting every item in list to integer

Discussion in 'Mac Programming' started by ohanian, Jun 4, 2011.

  1. macrumors newbie

    Joined:
    Apr 29, 2010
    #1
    -- I have a question regarding applescript

    -- I have a list of strings
    set mylist to {"1", "2", "3", "4", "5"}
    -- I want to convert to a list of integers

    set myint to every item in mylist as integer

    -- Try another way
    set myint to every item as integer in mylist

    --But both ways does not work
     
  2. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    AppleScript doesn't really have facilities to manipulate lists as a whole like that - the usual approach is to step through the list, making your coercions as you go.

    Code:
    set mylist to {"1", "2", "3", "4", "5"}
    
    repeat with x from 1 to (count mylist)
    	try -- skip errors
    		set item x of mylist to (item x of mylist as integer)
    	end try
    end repeat
    
    mylist -->  {1, 2, 3, 4, 5}
     
  3. thread starter macrumors newbie

    Joined:
    Apr 29, 2010
    #3
    thank you

    Thank you,

    Now my program works

    Code:
    -- Please note that home is a object in "Finder"
    -- It does not work outside of tell application
    tell application "Finder"
    	activate
    	set targetdirectory to folder "nobackup:zzz" of home
    	-- Now obtain a list of all files in the directory
    	set all_files_in_dir to every file in targetdirectory
    	-- Next convert the file from class document to string
    	repeat with x from 1 to (count all_files_in_dir)
    		try -- skip errors
    			set item x of all_files_in_dir to (item x of all_files_in_dir as string)
    		end try
    	end repeat
    end tell
    tell application "Preview" to open all_files_in_dir
    
     

Share This Page