Instantly share code, notes, and snippets.

Embed
What would you like to do?
AppleScript that generates list of files for the <resource> node in a SCORM manifest. Select the directory containing your content, and the script will generate the list, then copy to your clipboard. Results are formatted like this: <file href="index.html" />
(*
Based on http://www.jonn8.com/html/scripts/misc/List_Folder_Contents.html
with string replacement function from https://discussions.apple.com/message/20542403#20542403
*)
property script_name : "List Folder Contents"
property script_description : "This script will list a folder's contents returning full paths as strings and limit the list to specific file types. The script can also process subfolders (recursion)."
set the_folder to (choose folder with prompt "Choose a Folder to List:") as Unicode text
set file_types to {} --file types, set to {} to true to just return folders; file types are 4 character codes such as "osas" or "TEXT"
set posix_folder to POSIX path of (the_folder as Unicode text)
set add_to_clipboard to true
set the_files to get_folder_list(the_folder, file_types)
tell (a reference to my text item delimiters)
set {old_tid, contents} to {contents, return}
set {the_files_string, contents} to {the_files as Unicode text, old_tid}
end tell
set the_files_string to replaceText(the_files_string, posix_folder, "")
copy the_files_string to the_files
if add_to_clipboard then
set the clipboard to the_files_string
end if
beep
return the_files
(* functions *)
on get_folder_list(the_folder, file_types)
set the_files to {}
tell application "Finder" to set folder_list to items of folder the_folder
repeat with new_file in folder_list
try
set temp_file_type to file type of new_file
on error
set temp_file_type to "fold"
end try
if file_types contains temp_file_type or file_types = {} then
set this_item to POSIX path of (new_file as Unicode text)
-- <file href="index.html" />
if this_item does not end with "/" then -- exclude folders
set this_item to "<file href=" & quote & this_item & quote & " />"
set the_files to the_files & {this_item}
end if
end if
if temp_file_type = "fold" then
set the_files to the_files & my get_folder_list((new_file as Unicode text), file_types)
end if
end repeat
return the_files
end get_folder_list
on replaceText(someText, oldItem, newItem)
(*
replace all occurances of oldItem with newItem
parameters -
someText [text]: the text containing the item(s) to change
oldItem [text, list of text]: the item to be replaced
newItem [text]: the item to replace with
returns [text]: the text with the item(s) replaced
*)
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}
try
set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}
set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}
on error errorMessage number errorNumber -- oops
set AppleScript's text item delimiters to tempTID
error errorMessage number errorNumber -- pass it on
end try
return someText
end replaceText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment