Problem refreshing file name extensions in open/save windows

The following problem arose as I was writing a generic GUI script (that I would be happy to share if anyone is interested) that navigates through an application’s “Open” or “Save” dialog window and finds and selects a desired target file. The problem is best described by example:

  1. Open any application, for example “Text Edit” (any application will do)
  2. Open an “Open file” dialog window (File → Open… -or- command-“o”)
  3. Navigate to any folder (let’s call it “folder X”) that contains files and note the displayed name extensions of the files (visible or hidden depending on each file’s name extension visibility setting)
  4. Close the “Open file” dialog window (click “Cancel” -or- command-“.”)
  5. Change the name extension visibility of one or more of the files in folder X, ie, if hidden, make the extension visible, or if visible, make the extension hidden (using either the “Get Info” window or programmatically in AppleScript)
  6. Reopen the “Open file” dialog window in the application, and navigate back to folder X. NOTE THAT THE DISPLAYED NAME EXTENSIONS HAVE NOT REFRESHED TO REFLECT THE CHANGES IN VISIBILITY THAT WERE MADE, BUT RATHER CONTINUE TO SHOW THE OLD DISPLAYED NAME EXTENSIONS.

The reason why this is a problem is that two files with the same name but different name extensions will appear in the dialog window identically if both name extensions are hidden, and the GUI script won’t have any way to distinguish between the two files. I have found two ways to refresh the displayed name extensions, but both seem rather crude ways of accomplishing this task:

(Perform either of these between steps 5 and 6 in the example)

  1. Close then reopen the entire application
  2. Create then delete a random file or folder in folder X

Does anyone have suggestions on a more elegant way to refresh displayed name extensions in Open/Save dialog windows? Thank you for any help you can provide.

Hi

I can’t recreate your problem but this sounds like it might work for you!!

tell application "Finder" to update items of front window with necessity

picked this snippet up from somewhere.

pidge,

Thank you for the suggestion. Unfortunately, it didn’t fix my problem (even though it seemed like just the kind of command for the task.) I’m curious why you can’t reproduce the problem. I am running Mac OS 10.4.11 on a G4 PowerBook. Perhaps your computer or OS is more recent? It seems that what is needed is some event involving the target folder that signals to applications that the properties of files in the target folder have changed. I just can’t find that signal short of the two brute-force methods I mentioned (quitting then restarting the application, or adding and/or deleting a file in the target folder.)

I observed the same problem when trying to toggle a file’s “Hide extension” setting (both through the “Get Info” window and extension hidden property in Finder scripting). The filenames displayed in TextEdit’s Open. dialog were not updated unless I modified the containing folder or restarted TextEdit.

I have used the update command that pidge1 mentions, but it did not help when I applied it to this situation. The Finder window always seems to display the proper information (extension or no extension) (it updates in “real time” when the file extension settings are changed via AppleScript), so it does not really seem to need update. Mostly I have had to use the update command when changes are made by a “UNIX” program and I need to poke Finder to refresh its display.

I found that just changing the modification date of the containing folder is enough to get TextEdit to notice the changed “Hide extension” setting of the files that it lists in its Open. dialog. If the Open. dialog is already open, I had to close and reopen it or switch away from and back to TextEdit for the filesnames to be redisplayed (the selection and scrolling tricks that apply to the next method did not work for this one). This messes up the modification date, but that can be limited by only changing it by one second (both forward and backward worked for me). The modification date could also be restored when your script has finished poking at the UI of the Open. dialog. Without the restoring code it looks like this:

tell application "Finder"
	set m to modification date of fRef
	set modification date of fRef to m + 1
end tell

Another workaround that I found was to enable Finder’s “Show all file extensions” preference. With this, you do not have to modify each file’s extension setting. When I changed the “Show all file extensions” setting (either with the GUI or with AppleScript), the filenames displayed in TextEdit’s Open. dialog (nearly) automatically refreshed. If the dialog was already open I did have to poke it a little to get the changes to show up. There were many ways to get the display to refresh: closing and reopening the dialog; switching apps and switching back to TextEdit; doing a Select All; moving the selection through each item (either mouse clicks or keyboard commands); scrolling the list of files so that all the affected files are scrolled out of and back into the display. You can temporarily toggle the Finder preference with some AppleScript code like this:

to forceExtensionsDisplay()
	set enabledAllExtensions to false
	tell application "Finder" to tell Finder preferences
		if all name extensions showing is not true then
			set enabledAllExtensions to true
			set all name extensions showing to true
		end if
	end tell
	script
		to undo()
			tell application "Finder" to tell Finder preferences to if enabledAllExtensions and all name extensions showing is true then set all name extensions showing to false
		end undo
	end script
end forceExtensionsDisplay

set allExtRestorer to forceExtensionsDisplay()
try
	(* (Re)Open the Open. dialog and UI script it. *)
on error m number n from o partial result r to t
	allExtRestorer's undo()
	error m number n from o partial result r to t
end try
allExtRestorer's undo()

That has extra code to make the cleanup easier, but the core is just making changes to Finder’s all name extensions showing of Finder preferences property.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)

Edit history: Some minor formatting and wording changes.

chrys,

I can’t thank you enough for the outstanding analysis and description of solutions. What a thorough effort! I very much like the slight tweak to the modification date of the folder - quick, harmless, easy. I had also discovered the Finder “Show all file extensions” preference approach, but I noticed that it took an exorbitant amount of time for the Finder to implement the preference change. So I’ll stick with your “modification date” approach. It raises the question of whether there might be other changes in property values of the containing folder that would have the same desired effect.