I have to batch process a huge number of Quark files that will be in a folder, but can be located in any number of layers deep in sub-folders. I want my script to ask for what folder to process and then go through all the sub-folders and process only the Quark documents. I searched this site and also looked at other scripts I have written but can’t find the right code to do this. I am including the code I have so far. (The main Quark process is basically blank right now. I want to get the “skeleton” correct before filling in the details.) The main part I need help on is after selecting the folder to process. The System Events “set this_folder to…” line is totally wrong but it’s the last approach I tried. Is it best to use “System Events” for this or is there a better way in a “Finder” tell block?
Anyway, I am hopeful that someone knows an effective/efficient way to re-work this. Thanks.
set this_folder to (choose folder with prompt "Process all Quark files in what folder??")
tell application "System Events"
set these_files to every file of folder this_folder whose file type is "QuarkXPress Project File"
end tell
repeat with i from 1 to the count of these_files
set this_file to item i of these_files as alias
tell application "QuarkXPress"
activate
delay 1
open file this_file use doc prefs yes remap fonts ask do auto picture import no
tell front document
set tool mode to drag mode
end tell
close front document saving yes
end tell
end repeat
Weird… two recursion examples in two days! Here’s a core.
global dPath
tell application "Finder"
set dPath to (desktop as alias)
set dPath to (dPath & "filelist3.txt") as string
set chosenFolder to choose folder -- Pick your folder.
my folderprocess(chosenFolder) -- Send it to the recursive handler
-- When the script is done running the handler, it continues the flow as normal.
open alias dPath
end tell
on folderprocess(chosenFolder)
tell application "Finder"
set myFiles to every file of chosenFolder whose kind = "Adobe PDF document" -- Get a list of the documents in this folder (in case there are loose ones)
if (count of myFiles) ≠0 then -- If files are found, Send them to another handler to do something with them (in this case just logging the file paths)
my finderprocess(myFiles)
end if
set subFolders to folders of chosenFolder -- Get a list of the subfolders of the folder you picked
if (count of subFolders) ≠0 then -- If folders are found
repeat with thisFolder in subFolders -- Start repeating with a new list of subfolders
my folderprocess(thisFolder) -- send it back to the beginning of the handler
end repeat
end if
end tell
end folderprocess
on finderprocess(myFiles) -- Replace this process with any other one, like a Quark subroutine.
repeat with thisfile in myFiles
open for access dPath with write permission
set fileEOF to eof
write ((thisfile as string) & return) starting at eof to alias dPath
close access alias dPath
end repeat
end finderprocess
BTW, this will drill down on a per folder basis. For example, if you have a structure like this:
Folder 1: Folder 2: Folder 3: Folder 4
Folder 1: Folder 2.2: Folder 3.2
Folder 1: Folder 2.3
… Folder 2.3 will be the last folder processed. It starts in Folder 1 and follows through to the end of Folder 2’s contents. Then it leaps back and goes to the end of Folder 2.2’s contents, etc. (This may be affected by the sort order of the filenames but I’m too lazy to test for that right now!) Watch your Event Log History and run it on a small folder if you want.
Have fun with it. (Actually someone else may get some use out of it as a file lister. There seems to be enough requests for them!)
Jim Neumann
BLUEFROG
P.S. with the comments out and the extraneous lines for setting up the dummy file gone, this script won’t look so long or intimidating.
Thanks, Bluefrog. I merged what you had with what I needed and it works! I still have to write the main Quark part but the main process works great. I also added a counter so it would tell you how many files it ended up processing at the end. Here’s my code in case anyone can find something useful in it.
Thanks again for the help.
global TotalCount
tell application "Finder"
activate
set chosenFolder to (choose folder with prompt "Process all Quark files in what folder??")
set TotalCount to 0
my folderprocess(chosenFolder)
activate
beep 2
display dialog ("Total Quark files processed: " & TotalCount) buttons "OK" default button "OK"
end tell
on folderprocess(chosenFolder)
tell application "Finder"
set myFiles to every file of chosenFolder whose kind starts with "QuarkXPress"
if (count of myFiles) ≠0 then
my finderprocess(myFiles)
end if
set subFolders to folders of chosenFolder -- Get a list of the subfolders of the folder you picked
if (count of subFolders) ≠0 then -- If folders are found
repeat with thisFolder in subFolders -- Start repeating with a new list of subfolders
my folderprocess(thisFolder) -- send it back to the beginning of the handler
end repeat
end if
end tell
end folderprocess
on finderprocess(myFiles)
repeat with this_file in myFiles
tell application "QuarkXPress"
activate
open file (this_file as string) use doc prefs yes remap fonts ask do auto picture import no
tell application "System Events"
keystroke "0" using command down
end tell
delay 4
tell front document
set tool mode to drag mode
end tell
close front document saving yes
set TotalCount to TotalCount + 1
end tell
end repeat
end finderprocess
thanks for posting this script. i’ve adapted it to ‘touch’ all the folders and files in a directory to help out some users over on the Retrospect forum. i thought i’d post the code for all to see:
global myPass
global dPath
property passQ : "What is the admin password?"
set myPass to text returned of (display dialog passQ default answer "")
tell application "Finder"
set chosenFolder to choose folder
my folderprocess(chosenFolder)
end tell
do shell script "/bin/rm -rf /tmp/myTouch" password myPass with administrator privileges
display dialog "Your files and folders have been \"touch\"ed"
on folderprocess(chosenFolder)
set theTmp to "/tmp/myTouch"
set currFolder to chosenFolder as alias
set myTouch to "/usr/bin/touch " & quoted form of POSIX path of currFolder
do shell script "/bin/echo" & space & quoted form of myTouch & space & ">" & theTmp password myPass with administrator privileges
set doMyTouch to "/bin/sh" & space & theTmp
do shell script doMyTouch password myPass with administrator privileges
tell application "Finder"
set myFiles to every file of chosenFolder
if (count of myFiles) ≠0 then
my finderprocess(myFiles)
end if
set subFolders to folders of chosenFolder
if (count of subFolders) ≠0 then
repeat with thisFolder in subFolders
my folderprocess(thisFolder)
end repeat
end if
end tell
end folderprocess
on finderprocess(myFiles)
set theTmp to "/tmp/myTouch"
repeat with thisfile in myFiles
set currFile to thisfile as alias
set myTouch to "/usr/bin/touch " & quoted form of POSIX path of currFile
do shell script "/bin/echo" & space & quoted form of myTouch & space & ">" & theTmp password myPass with administrator privileges
set doMyTouch to "/bin/sh" & space & theTmp
do shell script doMyTouch password myPass with administrator privileges
end repeat
end finderprocess
If you just need a list of all Quark project files in a folder then you could just use the finder’s commands and get that:
set TheFolder to choose folder
tell application "Finder" to set FileList to (files of entire contents of TheFolder whose kind is "QuarkXPress Project File") as alias list
Which would return the following list:
{alias "Macintosh HD:Users:Me:Desktop:MainFolder:Project_Template", alias "Macintosh HD:Users:Me:Desktop:MainFolder:Project_Template copy", alias "Macintosh HD:Users:Me:Desktop:MainFolder:SubFolder1:Project_Template copy 1", alias "Macintosh HD:Users:Me:Desktop:MainFolder:SubFolder1:SubFolder2:Project_Template copy 2"}