PDA

View Full Version : Applescript, converting every item in list to integer




ohanian
Jun 4, 2011, 10:56 PM
-- 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



Red Menace
Jun 4, 2011, 11:35 PM
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.

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}

ohanian
Jun 5, 2011, 05:39 AM
Thank you,

Now my program works

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