I’m working on a script that will allow you to navigate Finder using voice recognition. My current plan is to use attached folder actions to create aliases of the folder’s contents to “Speakable Items” (removing previous aliases to prevent “overloading” a name).
on opening folder this_folder
(* First delete all alias files in the speakable items folder
Later versions of this script should be improved so that
they do not delete aliases created by the user on purpose. *)
tell application "Finder"
set speakable_items_folder to ((path to "dlib" from user domain as string) & "Speech:Speakable items")
tell application "System Events"
set these_files to every file of folder speakable_items_folder
end tell
repeat with i from 1 to the count of these_files
set this_file to (item i of these_files as alias)
set this_info to info for this_file
if visible of this_info is true and alias of this_info is true then
delete this_file
end if
end repeat
end tell
(* Create aliases of the folder items in the speakable items folder *)
tell application "Finder"
tell application "System Events"
set these_files to every file of folder this_folder
end tell
repeat with j from 1 to the count of these_files
set this_file to (item j of these_files as alias)
set this_info to info for this_file
if visible of this_info is true and alias of this_info is false then
make new alias file ¬
at alias ((path to "dlib" from user domain as string) & "Speech:Speakable items") ¬
to (this_file as alias)
end if
end repeat
end tell
end opening folder
I have verified that the top section works properly, but the bottom part doesn’t add new aliases. I’m still pretty new to AppleScript, so I suspect there’s something obvious that I’ve overlooked.
I have another problem concerning this script, however. I would like to delete only the aliases that have the comment: “created by SpeakableFinder” so that when I create other aliases that I want to keep permanently, they aren’t deleted. So far, I’ve tried iterating through the files, storing them in the variable “this_file” and using:
set this_info to info for this_file
if alias of this_info is true and comment of this_info contains "created by SpeakableFinder" then
delete this_file
I have also made sure that the aliases have the comment “created by SpeakableFinder.”
Can anyone tell me how to correctly access the comment property, and compare it to a string?
Seems like your diagnostic script worked out fine. Here are the event log results:
tell current application
path to speakable items from user domain
alias "Macintosh HD:Users:Matthew:Library:Speech:Speakable Items:"
end tell
tell application "Finder"
delete every alias file of alias "Macintosh HD:Users:Matthew:Library:Speech:Speakable Items:" whose comment contains "created by SpeakableFinder"
{alias file "Arabesque" of trash, alias file "Nostalgia" of trash, alias file "Prelude" of trash}
choose folder
alias "Macintosh HD:Users:Matthew:Music:GarageBand:"
get every file of alias "Macintosh HD:Users:Matthew:Music:GarageBand:" whose class ≠alias file
{document file "Arabesque.band" of folder "GarageBand" of folder "Music" of folder "Matthew" of folder "Users" of startup disk, document file "Nostalgia.band" of folder "GarageBand" of folder "Music" of folder "Matthew" of folder "Users" of startup disk, document file "Prelude.band" of folder "GarageBand" of folder "Music" of folder "Matthew" of folder "Users" of startup disk}
make new alias file at alias "Macintosh HD:Users:Matthew:Library:Speech:Speakable Items:" to {document file "Arabesque.band" of folder "GarageBand" of folder "Music" of folder "Matthew" of folder "Users" of startup disk, document file "Nostalgia.band" of folder "GarageBand" of folder "Music" of folder "Matthew" of folder "Users" of startup disk, document file "Prelude.band" of folder "GarageBand" of folder "Music" of folder "Matthew" of folder "Users" of startup disk} with properties {comment:"created by SpeakableFinder"}
{alias file "Arabesque" of folder "Speakable Items" of folder "Speech" of folder "Library" of folder "Matthew" of folder "Users" of startup disk, alias file "Nostalgia" of folder "Speakable Items" of folder "Speech" of folder "Library" of folder "Matthew" of folder "Users" of startup disk, alias file "Prelude" of folder "Speakable Items" of folder "Speech" of folder "Library" of folder "Matthew" of folder "Users" of startup disk}
end tell
The problem seems to have magically fixed itself… somehow. I tried outcommenting some things so that I could get the script to run until the end, and when I put them back it worked. For lack of anything else to blame, I’m going to assume I made a syntax error or a typo.
Here is my first working version:
on opening folder this_folder
--Delete alias files in the speakable items folder to avoid collision with other names
set speakable_items_folder to (path to speakable items from user domain)
if this_folder is not speakable_items_folder then --Do not try to make the Speakable Items Folder speakable
tell application "Finder"
delete (alias files of speakable_items_folder whose comment contains "created by SpeakableFinder")
--Create aliases of the folder's contents in the speakable items folder
make new alias file ¬
at speakable_items_folder ¬
to (get files of this_folder whose class is not alias file) ¬
with properties {comment:"created by SpeakableFinder"}
make new alias file ¬
at speakable_items_folder ¬
to (get folders of this_folder whose class is not alias file) ¬
with properties {comment:"created by SpeakableFinder"}
--Add "open " to the front of alias names
set the speakable_items_folder to speakable_items_folder as alias
set the item_list to list folder speakable_items_folder
set speakable_items_folder to speakable_items_folder as string
repeat with i from 1 to number of items in the item_list
set this_item to item i of the item_list
set this_item to (speakable_items_folder & this_item) as alias
set this_info to info for this_item
set the current_name to the name of this_info
if alias of this_info is true then
set the new_file_name to the ("Open " & the current_name) as string
set the name of this_item to new_file_name
end if
end repeat
end tell
end if
end opening folder
I know the “if” statement is mostly useless, unless I (or anyone else) isn’t thinking clearly and attaches the action to the Speakable Items Folder itself, but I’m a fan of “robust” code, so I’ve included it.
I’ve also written code to add "open " to the front of new alias names, because it seems more natural to say “Computer, open <file/folder>.”
My current way of doing this isn’t very elegant, so if anyone knows a better way, I’d be glad to learn it.