I alluded to this is a post yesterday, and have now confirmed it on two of my machines. Whenever you use the [choose file] command, and then use the Spotlight window to find the file you want, the script quits with the “User Canceled.” in the log. Try this:
set a to choose file
tell application "Finder" to get a's properties
Then type some generic letter pair in the Spotlight window and select a dislpayed file. This is what the log produces:
tell current application
choose file
“User canceled.”
Has anyone else encountered this, or found a work around?
Doesn’t happen to me as you describe in OS X 10.4.4 - but the script does fail to produce any result if I use the spotlight text box to find the file to choose.
Switch your Script Editor to the Event Log view, and try it again; you will get the “User Canceled” response, which is why no results are returned; the script does not see the chosen file.
Gotcha - that’s what I do see. Pretty clearly a bug. It doesn’t work when you put choose file inside the Finder tell block either.
I’ve never figured out either how to use the Finder’s “find” from a script. The dictionary says “find Unicode text : the query to execute (in Spotlight form)” but I’ve never had any success with it. Could be that you’ve got to find the file yourself unless you’re trying to find a file of a specific type. Then you can say: Choose file of type “scpt”, and you can always narrow the search by supplying a default location.
Thanks for the confirmation. I have been playing around with the [choose file file type] for a couple of days, but had little success. It’s too bad, even that would simplify things a little for me.
What would you like Choose File to do (aside from work with the spotlight text box)? There’s got to be a way to mimic the behavior you want with your own handler that puts up a dialog box and goes from there.
I want to find and select music files from my second hard drive, where I store my iTunes music. As you may know, iTunes develops its own folder structure, that can be rough to navigate through. I have a friend that enjoys the occasional receipt of an off the wall track every few days, and I have a few thousand to choose from, so I wrote a short script that allows me to choose a file track, and input a short message. Then it emails my friend. (Very slick, I might add.)
I am considering altering it into a droplet, so that I can just drag the track from any Finder window, or even from iTunes itself, and get the message prompt and then send from there.
Just to confirm that I also consider this a bug, and a fairly serious one - since there’s no way of preventing a user from conducting a spotlight search. A similar problem arises with the choose folder command - although that doesn’t even error (it just doesn’t return a result). I’ve submitted a bug report to Apple, detailing both issues, against Bug ID# 4434047.
The droplet idea sounds like a reasonable alternative, Craig. However, would something like this help in any way?
choose file of type {"public.mp3", "public.aifc-audio"} default location alias "path to required disk or folder here" without invisibles
That may help, I will certainly try it out. The issue really comes down to how iTunes organizes the folders that hold the mp3 files. Most tracks have a primary folder identified by the artist, then separate child folders listed by Album name. There is also a large group of folders contained in a folder entitled ‘Compilations.’ There seems to be no rhyme nor reason for which tracks end up in compilations.
So, there is still the hunt through the list of dozens of folders to find the desired track. I am intensely lazy, and would prefer to be able to find the track with Spotlight, since it searches the metadata, it is easier to find obscure tracks. So, I will mess with the droplet first, trying to drop files from a Finder window located with Spotlight and see if they can be seen. If so, I will proceed with the Mail script I have written and convert it over.
If not, I will utilize your input, kai, and move forward. Thanks to both of you for confirming this bug and offering potential solutions.
--Kimbo Music Send
on open drop_file
set bb to item 1 of drop_file--Only allow a single attachment, regardless of number of files dropped.
tell application "Mail"
set b to make new outgoing message with properties {sender:"casdvm@mrcodeboy.com", subject:"Music"}
tell b to make new to recipient
set content of b to ("Kimbo:
" & (text returned of (display dialog "Message Today:" default answer ""))) & return & return & "Craig" & return & return
tell b's content --Remember to reference the [b] variable, stupid!
make new attachment with properties {file name:bb} at after the last paragraph
end tell
set address of first to recipient of b to "mrcodeboy@mrcodeboy.com"
send b
end tell
end open
All I did was add the top and bottom [open] commands, limit it to the first item in the list (just in case I ever accidentally drop mulitple files), and it worked perfectly dragging a file from the Finder window that was located via Spotlight.