Apple Photo (media) Picker
Ken Ray
kray at sonsothunder.com
Fri Dec 9 11:25:28 EST 2011
On Dec 9, 2011, at 5:39 AM, Graham Samuel wrote:
> Hi Mark
>
> I am sure that would be a very fine way of doing it, but I have to ask myself whether it would be quicker for me to implement a picker in LiveCode (where I feel comfortable, and where at least some of the logic would be useful on other platforms) or learn AppleScript and Automator and then attach the result to a LiveCode program (way out of my comfort zone, but of course much more educational for me). I don't normally use AppleScript, and the last time I tried, I found it looked a lot like LiveCode but actually so different in detail that the whole experience was very frustrating… anyway thanks for the suggestion and I'll see if I've got the moral energy to proceed.
Here's what you do (I tested this):
In Automator:
1) Start a new workflow and add two actions: "Ask for Photos" (this is in the 'Photos' category under 'Library' in the left column) and "New Text File" (this is in the 'Text' category).
2) For "Ask for Photos", you can either leave it alone or uncheck "Allow multiple selection" (if you don't want the user to do that).
3) For "New Text File", set these settings (the rest you can just leave as defaults):
Save as: picturepath.txt
Where: Documents [x] Replacing existing files
4) Save the workflow as an application (you can pick from the 'FIle Format' popup menu at the bottom of the 'save as' dialog) named "Choose Picture".
NOTE: Save it to your desktop for now so the following LC script will work without modification.
In LiveCode, create a button and put this script in it:
on mouseUp
ChoosePicture
put the result
end mouseUp
on ChoosePicture
put 30000 into tTimeout
put specialFolderPath("documents") & "/picturepath.txt" into tPicPathFile
put specialFolderPath("desktop") & "/Choose Picture.app" into tChoosePicApp
if there is a file tPicPathFile then delete file tPicPathFile
launch tChoosePicApp
put the milliseconds into tMS
put true into tTimedout
repeat until ((the milliseconds - tMS) >= tTimeout)
wait 100 milliseconds with messages
if there is a file tPicPathFile then
put url ("file:" & tPicPathFile) into tPicPath
delete file tPicPathFile
put false into tTimedOut
exit repeat
end if
end repeat
if not(tTimedOut) then
if tPicPath = "" then return "Cancel"
return tPicPath
else
return "timeout"
end if
end ChoosePicture
Now click the button and test it out… you should get back in "the result" either the path to the selected picture, the word "Cancel" (if they chose to click the Cancel button), or the word "timeout" if for some reason it couldn't get the file that is supposed to be created by the Choose Picture app.
A couple of caveats/notes:
1) AFAIK there's no way to call the "Ask for Photos" palette from AppleScript directly, so that's the reason for the Automator solution.
2) When the "Ask for Photos" palette is open, the Automator "gear" icon is shown in the Mac menu bar (although there's nothing the user can do with it).
3) Feel free to change the paths to where you want to write the file, how it's named, etc… you get the idea on how this works…
Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/
More information about the use-livecode
mailing list