Hello.
Here are a pair of “generic” file open/save dialogs to use in cooperation with Standard Additions.osax’s file io routines.
They are localized and takes care of their own error handling, they never die but returns null
instead of returning a hfs pathName as text. So that you can do your errorhandling on a higher level.
They are assumed to work properly under Tiger as well. Use Unicode text instead of text.
[b][color=blue]openFile[/color][/b] : returns hfs pathname as text upon success, you must open the file for reading/writing.
[b][color=blue]SaveAs[/color][/b]: returns hfs pathname as text upon success, you must open it the file for writing. [b][color=blue]SaveAs[/color][/b] [b]creates[/b] the new file.
Both handlers supports a blnAskToConfirm parameter, for asking the user to confirm a cancellation the first time. The second cancellation is obeyed directly.
Best Regards
McUsr
-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
-- macscripter.net/viewtopic.php?id=33546
(*
TERMS OF USE.
This applies only to posting code, as long as you don't post it, you are welcome to do
whatever you want to do with it without any further permission.
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code.
You must also state what you eventually have done with the original source. This obviously doesn't matter if you distribure AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code.
Credit for having contributed to your product would in all cases be nice!
If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.
The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.
I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code.
The above paragraphs are also valid if you violate any of my terms.
If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.
*)
-- its a generic handler for saving a file which can ask if your really want to abort, the save operation or just returns from the handler otherwise.
-- PARAMETERS:
-- txtTitle
-- The title you want to show up in the SaveAs dialog.
-- pxStartPathAsText
-- The initial Posix path you want to show up.
-- blnAskToConfirm
-- if you want the user to confirm the cancel, the dialog will only show up once, next time straight out.
-- RETURNS
-- The hfs Path as text upon sucess, null otherwise.
on SaveAs(txtTitle, pxStartPathAsText, blnAskToConfirm)
local new_path, blnUserQuit, iConfirmTimes
set blnUserQuit to false
set iConfirmTimes to 0
tell me
activate
repeat while true
tell application "System Events"
tell application process "Finder"
activate
try
set new_path to (choose file name with prompt txtTitle default location (POSIX file pxStartPathAsText))
exit repeat
on error e number n
-- display dialog "err: " & e & " : " & n
if n is -128 then
set iConfirmTimes to iConfirmTimes + 1
try
if blnAskToConfirm is true and iConfirmTimes < 2 then
set theButton to the button returned of (display dialog "Do you want to try again?" with title txtTitle with icon 2)
-- falls through the end of the repeat loop and repeat ourselves.
else -- not talking back
set blnUserQuit to true
exit repeat
end if
on error -- user hit "cancel" or equiv in the dialog.
set blnUserQuit to true
exit repeat
end try
end if
end try
end tell
end tell
end repeat
end tell
if not blnUserQuit then
try
set fp to open for access new_path with write permission
on error e number n
display alert (txtTitle & " an error occured: " & e & " : " & n)
return null
end try
try
close access fp
on error e number n
try
close access fp
on error
display alert (txtTitle & " an error occured: " & e & " : " & n)
return null
end try
end try
set new_path to new_path as alias as Unicode text
return new_path
else
return null
end if
end SaveAs
-- its a generic handler for opening a file which can ask if your really want to abort the open operation or just returns from the handler otherwise.
-- PARAMETERS:
-- txtTitle
-- The title you want to show up in the SaveAs dialog.
-- pxStartPathAsText
-- The initial Posix path you want to show up.
-- blnAskToConfirm
-- if you want the user to confirm the cancel, the dialog will only show up once, next time straight out.
-- RETURNS
-- The hfs Path as text upon sucess, null otherwise.
-- openFile("Open a File", "/", true)
on openFile(txtTitle, pxStartPathAsText, blnAskToConfirm)
local new_path, blnUserQuit, iConfirmTimes
set blnUserQuit to false
set iConfirmTimes to 0
tell me
activate
repeat while true
tell application "System Events"
tell application process "Finder"
activate
try
set new_path to (choose file with prompt txtTitle default location (POSIX file pxStartPathAsText) of type {"TEXT"})
exit repeat
on error e number n
-- display dialog "err: " & e & " : " & n
if n is -128 then
set iConfirmTimes to iConfirmTimes + 1
try
if blnAskToConfirm is true and iConfirmTimes < 2 then
set theButton to the button returned of (display dialog "Do you want to try again?" with title txtTitle with icon 2)
-- falls through the end of the repeat loop and repeat ourselves.
else -- not talking back
set blnUserQuit to true
exit repeat
end if
on error -- user hit "cancel" or equiv in the dialog.
set blnUserQuit to true
exit repeat
end try
end if
end try
end tell
end tell
end repeat
end tell
if not blnUserQuit then
return new_path as Unicode text
else
return null
end if
end openFile