Script for determining whether a folder is open or not

I have a problem with regards to determining whether a folder or application is open or not. :frowning: I have this code but it is not working.

  [color=blue]tell application "finder"
      set fol to "mydrive:users:desktop:foldr"
      if fol is open then
              display dialog fol & ("is open)
      else
               display dialog fol & ("is close)
      end if
  end tell[/color]

Please help. Thank you.

I’m not sure if there is a property of the window itself that you can read directly to know if it is open but you can still find out by asking the Finder to list all of it’s windows and see if the name of the target folder matches the names of any of the open windows:

Jon

Here’s another way to test for the window. It might have a little less overhead because it doesn’t require Finder to get the name of every open window.

set fol to "mydrive:users:desktop:foldr"

set tids_ to AppleScript's text item delimiters
try
	set AppleScript's text item delimiters to ":"
	set folName to last text item of fol
	set AppleScript's text item delimiters to tids_
on error
	set AppleScript's text item delimiters to tids_
end try

tell application "Finder" to set winExists to exists window folName
if winExists is true then
	display dialog folName & " window is open"
else
	display dialog folName & " window is closed"
end if

And this will check to see if an application is running.

set appname to "Finder"

tell application "System Events" to ¬
	set exists_ to exists application process appname

if exists_ is true then
	display dialog appname & " is running"
else
	display dialog appname & " is not running"
end if

Both of our solutions were flawed because they didn’t account for the fact that two windows of different folders can have the same name (e.g., “path:to:folder” and “path:to:folder:folder” have the same name while being different folders and the Finder will not differentiate between them when simply taking into account the folder name). This script fixes that:

Jon

Good observation Jon! I should know better than to respond before consuming the minimum daily requirements of coffee. :shock:

Not content to count and loop through all windows, I’ve modified your script to narrow it down to work only on windows with the designated name. Let’s see if that second cup of coffee has helped. :wink:

set the_folder to (path to desktop as string) & "test"
set the_folder to the_folder as string
if character -1 of the_folder is not ":" then set the_folder to the_folder & ":"

tell application "Finder"
	try
		set candidates to name of windows whose name is (get name of alias the_folder)
	on error
		return "closed"
	end try
	
	set window_count to count candidates
	repeat with i from 1 to window_count
		try -- a column window with nothing selected will return an error so I trap it here 
			if (target of window i as string) = the_folder then return "open"
		end try
	end repeat
	
	return "closed"
end tell

– Rob

The value of the above scripts notwithstanding, I have to wonder what purpose they serve. Why would you ever need to know if a folder window is open or not?

About the only reason I can think of is so that you can either open if it is closed, or close it if it is open, both of which can easily be handled by either just opening the folder window (if it’s already open, you’re set), or using a try block to close it (letting the error handler ignore the error you get if the folder isn’t open). Either way you get the result you want without needing to know its prior state.

Thank you for all your replies. I tried each script and it works. By the way, to answer the query why do I need to know whether a folder or application is open is because I need it for the program I am developing. I have to make sure that nothing is opened. Again thanks. :smiley:

tell application "Finder" to close every window

Now you know nothing is open :slight_smile: