Script that ask for a file name (not save the file)

Hi all

I am looking for a script that ask for a file name (not save the file)

this will work almost what I want

tell application “Microsoft Excel”
set theName to choose file name with prompt “Enter the file name” default name “test.xls” default location file “Macintosh HD”
end tell

But I want to say for example that it only can enter a xls file for example
Can I use a file filter ?

Hello.

If you look in the Scripting Additions’s dictionary you’ll see that choose file takes an of type parameter.

I think you can specify both {“xlsx”} or {“com.microsoft.excel”} as filetypes in your case. (Not tested, I may have gotten the uti wrong.)

No, choose file name doesn’t provide a filter.
You have to do it programmatically for example to prompt the user to enter a name without extension
and add the extension later

Damn, love to duplicate VBA GetSaveAsFileName on a Mac with the same options as in Windows.

Your suggestion I use now in my new Excel/Outlook addin for the Mac
http://www.rondebruin.nl/mail/helprdbmailmac.htm

Thanks

Hello.

The common pattern is, to just add the extension you need if it isn’t there, and you can just check if the filename ends with your extension. It is quite easy. :slight_smile:

Actutally if there is a 3 letter or so wrong extension, or something that can be taken as a file extension, you are supposed to ask if the user will keep


set yourext to "xlsx"

set the fn to (choose file name) as text

set extStart to offset of "." in ((reverse of characters of fn) as text)

if extStart is not 0 then
	set spos to (length of fn) - extStart + 2
	set curExt to text spos thru -1 of fn
	
	if curExt is not yourext then
		try
			tell application "SystemUIServer"
				activate
				set theChoice to button returned of (display dialog "The current extension doesn't match \"." & yourext & "\". How shall we proceed?" buttons {"Cancel", "Add", "Replace"} cancel button 1 default button 3 with icon 2)
			end tell
		on error
			error number -128
		end try
		if theChoice = "Add" then
			set fn to fn & "." & yourext
		else
			
			set fn to text 1 thru (spos - 1) of fn & yourext
		end if
	end if
else
	set fn to fn & "." & yourext
end if

Thanks McUsr

Will play with this script

What I like of VBA GetSaveAsFileName is that you not able to see certan formats (you can set them) in Windows.
It is a shame that it not work correct on a Mac.

Have a great weekend

Hello!

The dialogs in bare Apple Script are somwhat simple, but they conforms to the User Interface Guidelines.

Basically a user is in his on right to choose the extension he pleases here. (Just try to save a html page as text in Safari for instance.

You will then get a question, about keeping or adding the mandatory. I figured this was the easiest, since now you don’t have to change the uti of the file. And it isn’t perfect, I don’t think it handles cases where the filename starts or ends with “.” very well.

(If I recall it right, there are really to schemes for this, one where the extension is mandatory, the other where it just gains.)

I did try to emulate that behaviour, since that is something that can be handy to have in my library anyway. (With some rework in the afternoon.)

And I wouldn’t trade AppleScript for VBA, for anything in the world! :smiley:

Have a good weekend you too.

Unlike VBA which is restricted to Microsoft Office, AppleScript is more all-purpose and the save dialog box works for all
file types.

If you want to accept only .xlsx extension, write a few lines of code which do
¢ if the user enters no extension, add one

  • if the user enters not the proper extension, replace it

Hi Stephan

No problem to make itlike this but it is not good looking.

In Windows you can set the filter on the extension you want and also not able to see the other formats.

For Selecting files(GetOpenFileName in VBA) I create a workeround like this in VBA
http://www.rondebruin.nl/mac.htm#GetOpenFilename

It is not easy on the Mac

Thanks for all your help and have a great weekend

Hello!

This scheme is much better looking for the user.

Say if I wanted to organize my spreadsheets by Quarter: then I could save a file as expensens.quarter 1. Leaving it up to me to organize my files as I want them. Excel is still the program used to open the file, as this is not totally controlled by the extension of the file, but of the uti of the file (uniform type identificator.)

It is really only in rare cases, where the extension, leads to another uti being set, that there is to be questioning about the file extension, as the setting of the uti is something that is really up to the launchservices (I think).

This approach is user freedom, on this platform the computer conforms to us, and not the other way around as they do on inferior platforms. :wink:

Hello

Here is a script building a table of the numerous file identifiers accepted by Excel.


(path to applications folder as text) & "Microsoft Office 2011:Microsoft Excel.app:Contents:Info.plist"

tell application "System Events"
	property list file result
	property list item "CFBundleDocumentTypes" of result
	set theRecords to value of item 1 of result
end tell

set typeNames to {{"CFBundleTypeName" & tab & "Unified Type Identifiers"}, {}}

repeat with i from 1 to count theRecords
	set recordi to item i of theRecords
	try
		set typeName to CFBundleTypeName of recordi
	on error
		set typeName to ""
	end try
	try
		set contentType to LSItemContentTypes of recordi
	on error
		set contentType to ""
	end try
	if {typeName, contentType} is not {"", ""} then
		set end of typeNames to my recolle({typeName, contentType}, tab)
	end if
end repeat
set theReport to (path to desktop as text) & "excel filetypes.txt"
my writeTo(theReport, my recolle(typeNames, linefeed), text, false)

tell application "Microsoft Excel"
	activate
	open file theReport
end tell

--=====

on recolle(l, d)
	local oTIDs, t
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to d
	set t to "" & l
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

--=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeTo(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		set openFile to open for access file targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeTo

--=====

I edited a bit to report three entries with partial datas.

Yvan KOENIG (VALLAURIS, France) samedi 6 octobre 2012 19:12:15

Hello!

I was just thinking of this, to make a general solution, and that solution would be to pass a list of acceptable type identifiers, save the file, by “open for access” and see what kind of type identifier was returned.

And if it was something else than “public-text” or whatever is the default for an empty text file, I’d go back and ask the user if he wants to add the extension I need, or keep it as it is. Then it is up to the user, and the fault is totally at the user. Microsoft Excel lets you save a spreadsheet as a text file, with the extension .txt, that is how the Operating System sees it anyway. (I just tried it.) Whereas Safari will ask you, if you add an extension of text, and maybe also if you add an extension of 3d quarter or something. Then I think it is either ok, to just silently add the extension, or modify the uti hinsightly.

As if the user starts the filename with a dot, then I’ll ask if he wants to create an invisible file. If that is his intentions, then I’ll let him or her.

This theme hasn’t quite slipped my mind, obtaining a correct name to save a file under, is indeed an important endeavour in a script.

There are several things to consider when getting a new filename to save under.

The most important is whether we are within the scope of an application when the saving of the data takes place or not.

The second one, is to stay within the behaviour of the app we are assisting.

As long as we are inside the scope of an application, telling the app to save the file under a name with an extension, it will either ignore the lack of an extension, or silently add it, also let the file be saved with the extension of the users choice (expenses.1 quater.2012), still setting the correct uti of the file. I think this must be tested on the app in speak before hand. Some apps, like Safari, balks at “customized extensions”, while others, like Microsoft Excel accepts them. So there is no universal scheme to be had.

Should we create and save a file outside the scope of an app, but still want it to conform to an app with regard to opening the file, without a specific extension, then there are certain things to consider.

The most important consideration is whether we are willing to go to the efforts of installing Satimage.Osax at a user’s machine in order to be sure, that the renaming of files can be a little bit more liberal, as Satimage.Osax is capable of setting the uti of a file. I have found no other Osax’en or other way to perform this particular operation.

Along side this is the fact that the only other way to bypass the lack of a way to set a uti is to set the default application of the file. This may break however if the user removes the .DS_Store file, since this information appears to be stored in the .DS_Store file of the folder where the file resides.

Conclusion

My assumption is that you want to be sure that the file can be opened with the app you have designated for the file.

Should you save the file within the scope of the app, then you can let the user choose any name he or she wants for the file. Provided the app lets you do so manually.

Should you save the file outside the scope of the application, then you can still let the user choose any name with any extension he or she likes, provided that you are willing to acertain that Satimage.Osax is installed, so that you can set the uti for the file directly “inside” the file.

Given the lack of Satimage.Osax is accepted, and that users sometimes removes their .DS_Store files, I’d recommend not allowing any filename without the proper extension for the uti of that filetype, since the extension of a file are the most important way now adays for the launch services to decide what application to use to open it with, when no uti are set explicitly for the file.

Hello!

There is one fact that I forgot to consider when writing the post above, and that is the very important one of cross platform compatibility.

Reading Apple Human Interface Guidelines made it all clear to me. :slight_smile:

In cases with apps like Excel, you really want to keep the xcls extension, so that it can be opened as what it is on other platforms.