FINAL SAMPLE - get every document file of every folder of startup disk
FINAL SAMPLE - get every document file of every folder of startup disk
- Subject: FINAL SAMPLE - get every document file of every folder of startup disk
- From: Bill Hernandez <email@hidden>
- Date: Wed, 20 Dec 2006 03:55:50 -0600
on script_title()
(*
Filename : finder_sel_final.scpt (Script Debugger)
Author : Bill Hernandez <email@hidden>
Author : Deivy Petrescu <email@hidden>
Version : 1.0.0
Updated : Wednesday, December 20, 2006 ( 1:34 AM )
*)
end script_title
-- +---------+---------+---------+---------+---------+
on script_notes()
(*
Deivy,
This is the finished version of the problem we've been working on...
It's Applescript friendly, so you can copy the whole thing to
your editor
This is the sample folder hierarchy I used for the tests . You
can see that there are three files
whose name contains ".txt", and two files whose creator is
"MSIE", the main folder, and a subfolder
A_TEMP <-- folder
A_msie_file.html <-- Creator "MSIE"
A_text_file_01.txt <--
Creator ""
A_text_file_02.txt <--
Creator ""
B_TEMP <-- folder
B_msie_file.html <-- Creator "MSIE"
B_text_file.txt
<-- Creator ""
To run the first test "select a few files at the Finder level",
the rest of the tests rely upon "Choose Folder",
so if you can create a similar hierarchy to what I show above,
and step through the debugger, you will see
the files change icon. If you are not using script debugger, I
put a dummy ShowMessage(...) after each call
to set the "file type" and "file creator", that way you can see
the changes...
If you have Script Debugger, you can comment out ShowMessage(...)
if you wish
and put breakpoints at each filelist_SetCreator(...) so you can
see the changes in the file icons
This works very well, thanks to all the help I received from
Deivy Petrescu...
It's purpose is to allow the user to select files at the finder
level, or via a "ChooseFolder" command, and
process the list. Files are added to the list either because they
fall into the {type_list, creator_list, or ext_list}.
There are two functions that use the "Choose Folder" paradigm
( 1 ) filelist_ChooseFolder(whichField, aValues, openTheFolder,
whichChoice)
( 2 ) filelist_GetFileList(whichField, aValues, openTheFolder,
whichChoice)
( 3 ) The third function sets the "file type" and "file creator"
to whatever you want, the property g_default_creator is set to "R*ch"
filelist_SetTypeCreator(aSel, myType, myCreator, setType_TF)
-- {"use_whatever_type_you_want", "" -> g_default_type, "clear" --
> ""}
-- {"use_whatever_creator_you_want", "" -> g_default_creator,
"clear" --> ""}
( parm 1) the list
( parm 2 ) for myType
( a ) you can pass whatever type you want
( b ) If you pass "", the function will use the default type
( c ) If you want to clear the type as is often needed for unix
scripts then pass "clear" and the function will remove the type
( parm 3 ) for myCreator
( a ) you can pass whatever creator you want
( b ) If you pass "", the function will use the default creator
( c ) If you want to clear the creator as is often needed for
unix scripts then pass "clear" and the function will remove the creator
( parm 4) (true -> set the file type, false -> ignore the file type}
If you try to clear the "file type", or "file creator" with an
empty string "", or with " " four spaces you will get an error.
Here's how I get around the problem :
set hard_space to (ASCII character 202)
set empty_val to (hard_space & hard_space & hard_space & hard_space)
set myType to empty_val
set myCreator to empty_val
At first I didn't bother to set the {file type : "TEXT"} because
I assumed it is already set to that, the extension "txt"
takes care of the issue, the important part is having the
correct application launch the document when double clicked,
but then I decided it was just as easy to add it, just in case...
*)
end script_notes
-- +---------+---------+---------+---------+---------+
property g_default_type : "TEXT"
property g_default_creator : "R*ch" --> BBEdit
property g_continue : true
-- +---------+---------+---------+---------+---------+
on run
set type_list to {"TEXT"}
set creator_list to {"MSIE", "sfri"}
set ext_list to {"php", "js", "incl", "html", "shtml", "htm",
"txt", "conf", "text", "code"}
set g_continue to true
-- -----------------------------------------------
-- ( 1 ) NOTICE THIS
set s to ""
set s to s & "( 1 ) USES THE FINDER SELECTION within -->
filelist_GetFileList( ...)" & return
set s to s & "includes any files whose 'creator' is in the
creator_list" & return
set s to s & "does not open the window of the selected folder
( it's already open since you are using the selection)" & return
set s to s & "sets the 'file creator' to Explorer, and ignores
the 'file type'" & return
if (g_continue) then
set aList to my filelist_GetFileList("creator", creator_list,
false, "selection") -- {"selection","choose"}
my filelist_SetCreator(aList, "TEXT", "MSIE", false)
ShowMessage(s, 25)
end if
-- -----------------------------------------------
set s to ""
set s to s & "( 2 ) USES 'choose folder' within -->
filelist_ChooseFolder( ...)" & return
set s to s & "includes any files whose 'extension' is in the
ext_list" & return
set s to s & "opens the window of the selected folder" & return
set s to s & "sets the 'file creator' to Safari, and ignores the
'file type'" & return
if (g_continue) then
set aList to my filelist_ChooseFolder("extension", ext_list,
true, "choose") -- {"selection","choose"}
my filelist_SetCreator(aList, "TEXT", "sfri", false)
ShowMessage(s, 25)
end if
-- -----------------------------------------------
set s to ""
set s to s & "( 3 ) USES 'choose folder' within -->
filelist_GetFileList( ...)" & return
set s to s & "includes any files whose 'creator' is in the
creator_list" & return
set s to s & "opens the window of the selected folder" & return
set s to s & "sets the 'file creator' to FireFox, and ignores the
'file type'" & return
if (g_continue) then
set aList to my filelist_GetFileList("creator", creator_list,
true, "choose") -- {"selection","choose"}
my filelist_SetCreator(aList, "TEXT", "MOZB", false)
ShowMessage(s, 25)
end if
-- -----------------------------------------------
set s to ""
set s to s & "( 4 ) USES 'choose folder' within -->
filelist_GetFileList( ...)" & return
set s to s & "includes any files whose 'type' is in the
type_list" & return
set s to s & "opens the window of the selected folder" & return
set s to s & "clears the 'file type' and 'file creator'" & return
if (g_continue) then
set aList to my filelist_GetFileList("type", type_list, true,
"choose") -- {"selection","choose"}
my filelist_SetCreator(aList, "clear", "clear", true)
ShowMessage(s, 25)
end if
-- -----------------------------------------------
set s to ""
set s to s & "( 5 ) USES 'choose folder' within -->
filelist_GetFileList( ...)" & return
set s to s & "includes any files whose 'extension' is in the
ext_list" & return
set s to s & "opens the window of the selected folder" & return
set s to s & "sets the 'file type' to 'TEXT' and 'file creator'
to BBEdit" & return
if (g_continue) then
set aList to my filelist_GetFileList("extension", ext_list,
true, "choose") -- {"selection","choose"}
my filelist_SetCreator(aList, "TEXT", "R*ch", true)
ShowMessage(s, 25)
end if
end run
-- +---------+---------+---------+---------+---------+---------
+---------+---------+---------+---------+
on filelist_ChooseFolder(whichField, aValues, openTheFolder,
whichChoice)
tell application "Finder"
try
if (whichChoice = "selection") then
set aSel to selection
else
set folder_ref to choose folder with prompt "Where are
your files stored?"
if (openTheFolder) then
activate
open folder folder_ref
end if
if (whichField = "extension") then
set aSel to (get (every document file of (every
folder) of folder_ref) whose name extension is in aValues) & (get
(every document file of folder_ref) whose name extension is in aValues)
end if
if (whichField = "type") then
set aSel to (get (every document file of (every
folder) of folder_ref) whose file type is in aValues) & (get (every
document file of folder_ref) whose file type is in aValues)
end if
if (whichField = "creator") then
set aSel to (get (every document file of (every
folder) of folder_ref) whose creator type is in aValues) & (get
(every document file of folder_ref) whose creator type is in aValues)
end if
end if
on error
set aSel to {}
end try
return aSel
end tell
end filelist_ChooseFolder
-- +---------+---------+---------+---------+---------+---------
+---------+---------+---------+---------+
on filelist_GetFileList(whichField, aValues, openTheFolder, whichChoice)
tell application "Finder"
try
if (whichChoice = "selection") then
set aSel to selection
else
set folder_ref to choose folder with prompt "Where are
your files stored?"
if (openTheFolder) then
activate
open folder folder_ref
end if
if (whichField = "extension") then
set aSel to (get every file of entire contents of
folder_ref whose name extension is in aValues)
end if
if (whichField = "type") then
set aSel to (get every file of entire contents of
folder_ref whose file type is in aValues)
end if
if (whichField = "creator") then
set aSel to (get every file of entire contents of
folder_ref whose creator type is in aValues)
end if
end if
on error
set aSel to {}
end try
return aSel
end tell
end filelist_GetFileList
-- +---------+---------+---------+---------+---------+---------
+---------+---------+---------+---------+
on filelist_SetCreator(aSel, myType, myCreator, setType_TF)
tell application "Finder"
set hard_space to (ASCII character 202)
set empty_val to (hard_space & hard_space & hard_space &
hard_space)
if (myType = "") then
set myType to g_default_type -- You can always set myCeator
to "clear" to get rid of the creator
else if (myType = "clear") then
set myType to empty_val
end if
if (myCreator = "") then
set myCreator to g_default_creator -- You can always set
myCeator to "clear" to get rid of the creator
else if (myCreator = "clear") then
set myCreator to empty_val
end if
if (setType_TF) then
repeat with theItem in aSel
set the file type of theItem to myType
set the creator type of theItem to myCreator
end repeat
else
repeat with theItem in aSel
set the creator type of theItem to myCreator
end repeat
end if
end tell
end filelist_SetCreator
-- +---------+---------+---------+---------+---------+---------
+---------+---------+---------+---------+
on ShowMessage(str, how_long)
tell application "Finder"
activate
if (how_long = 0) then
set myDelay to 5
else
set myDelay to how_long
end if
set b1 to "Cancel"
set b2 to "OK"
set aResult to display dialog str buttons {b1, b2} default
button {b2} giving up after myDelay
if (button returned of aResult = b1) then
set g_continue to false
end if
end tell
end ShowMessage
-- +---------+---------+---------+---------+---------+---------
+---------+---------+---------+---------+
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/mailman//archives/applescript-users
This email sent to email@hidden