Applescript file loop

I have a script that I've written below I would like to repeat the script for multiple files ie drop multiple files onto the Droplet and the action below is carried out on all the files.


I've had a stab at repeat/loop but to no avail I've copied others and cobbled the script together to use the name of a file to set the creation date on the file:-


The script works fine as a droplet on a single file however if I drop multiple files onto it I get an error.


This is my frist stab at an Applesrcipt so reckon Im missing out something pretty straightforward.


Any ideas how to amend the below?


on open theFiles

set theName to name of (info for theFiles)

set yy to characters 1 through 2 of theName as text as integer

if yy < 10 then set yy to "0" & yy

set mm to characters 3 through 4 of theName as text as integer

if mm < 10 then set mm to "0" & mm

set dd to characters 5 through 6 of theName as text as integer

if dd < 10 then set dd to "0" & dd

set hh to characters 8 through 9 of theName as text as integer

if hh < 10 then set hh to "0" & hh

set mn to characters 11 through 12 of theName as text as integer

if mn < 10 then set mn to "0" & mn

do shell script "touch -t " & yy & mm & dd & hh & mn & " " & quoted form of POSIX path of theFiles

end open

Macbook Pro, Mac OS X (10.4.10), 15" Screen

Posted on Oct 14, 2011 2:34 AM

Reply
2 replies

Oct 14, 2011 8:31 AM in response to gav_s

Your problem is that you're trying to work on all files en masse, and that won't work.


For example, when you:


set yy to characters 1 through 2 of theName as text as integer


which file name do you expect this to work on?


What you need to do is add a repeat loop to iterate through the files in turn. Fortunately it's trivial to do:


on opentheFiles

repeat with eachFile in theFiles

set theName to name of (info for eachFile)

set yy to characters 1 through 2 of theName as text as integer

if yy < 10 then set yy to "0" & yy

set mm to characters 3 through 4 of theName as text as integer

if mm < 10 then set mm to "0" & mm

set dd to characters 5 through 6 of theName as text as integer

if dd < 10 then set dd to "0" & dd

set hh to characters 8 through 9 of theName as text as integer

if hh < 10 then set hh to "0" & hh

set mn to characters 11 through 12 of theName as text as integer

if mn < 10 then set mn to "0" & mn

do shell script "touch -t " & yy & mm & dd & hh & mn & " " & quoted form of POSIX path of eachFile

end repeat

end open

So you can see, wrapping the commands in a 'repeat/end repeat' block runs the enclosed code once per item in the list. Additionally, when you say:


repeat with eachFile in theFiles

AppleScript uses eachFile as a reference to each item in the list each time - so the first time through the loop, eachFile points to the first item, the second time through it points to the second item, and so on, making it easy to iterate through a list of items.

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

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