RE: Manipulating multiple files in arbitrary applications
RE: Manipulating multiple files in arbitrary applications
- Subject: RE: Manipulating multiple files in arbitrary applications
- From: email@hidden
- Date: Tue, 13 Aug 2002 17:26:30 EDT
Dear Hamish,
There are two separate actions occuring here: one is selecting all the
files within a folder (and then selecting all the files within each
subfolder), and the other is opening and saving the files. Opening and saving
the files once you have their path names (i.e. -- the full name of where they
are) is no big deal, and it would seem that you have that part down pat. The
issue that you are likely to run into is dealing with sub-folders.
The easy way to deal with subfolders is recursion (calling a handler,
better known as a subroutine or function in most programming languages) for
each subfolder the same handler encounters. Example (should work as-is if you
correct the app section in the cente:)
choose folder with prompt "Please select the folder to do work onb&"
set OurFolder to the result
my HandlerWorkTheFolder(OurFolder)
----------------+----------------+----------------+----------------+----------
------+
-- HANDLER b IsFolder
-- Purpose: Determines if the current file is a folder (returns boolean)
-- Expected Input: String or alias of path&filename
-- Returns Output: Boolean, TRUE or FALSE
-- Calls NO other handlers
----------------+----------------+----------------+----------------+-------
---------+
local ValueForReturn -- boolean, Is path pointing at a folder?
-- local IAmAFolder -- boolean, value from file record's folder attribute
local TheRecordContents -- record, the complete contents of the file record
-- local LocalCopyOfFullItemPathAndName -- string, a local copy of the
FullItemPathAndName coerced into string
on IsFolder(FullItemPathAndName)
Tell Application "Finder"
set TheRecordContents to (get info for (FullItemPathAndName)) as list
-- item 7 in the list will be TRUE if it is a folder, FALSE if it is not
(boolean)
end tell
set ValueForReturn to (item 7 of (TheRecordContents)) as boolean
return ValueForReturn as boolean
end IsFolder
----------------+----------------+----------------+----------------+----------
------+
-- HANDLER b HandlerWorkTheFolder
-- Purpose: Works thru the contents of a folder, calling itself for subfolders
-- Expected Input: String or alias of path to folder
-- Returns Output: none
-- Calls Handler: IsFolder
----------------+----------------+---------
-------+----------------+----------------+
on HandlerWorkTheFolder(FolderPath)
set ListOFiles to list folder FolderPath
if ListOFiles is not {} then -- if the folder isn't empty:
set TotalCount to the count of items in ListOFiles
repeat with GenericLoopCounter from 1 to TotalCount
set CurrentFile to the (item GenericLoopCounter in ListOFiles as string)
set OriginatingPath to (FolderPath & CurrentFile) as string
if (my IsFolder(OriginatingPath)) then -- if the source is a folder
set OriginatingPath to (OriginatingPath & ":") as string
HandlerWorkTheFolder(OriginatingPath)
else
tell application "BBEdit 5.1"
activate -- this may or may not be necessary, depending on the app
set LocalFile to open OriginatingPath
save LocalFile -- may require 'with replace' or 'to OriginatingPath'
depending on the app
close LocalFile
end tell
end if
end repeat
end if
end HandlerWorkTheFolder
Best Wishes,
=-= Marc S.A. Glasgow
Mac Consultant since 1988 (Tampa, FL)
Hamish Wrote:
>
>
What I want to do is simple. For a collection of files
>
(e.g., traverse a folder and its subfolders), open them in
>
the application, and simply save them again (the purpose of
>
this exercise is to ensure that their file associations are
>
correct - the files in question were created on a PC).
>
>
My pseudo-code would look something like this:
>
>
for filename in "folder" plus subfolders tell application
>
"myapp" open filename save as filename end tell end for
>
>
Could anyone tell me whether this is possible, and if so
>
whether I need any extensions, and where I might find some
>
example code for doing this?
>
>
Many thanks in advance,
>
Hamish
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.