C-Command Software Forum

AppleScript to check if library is open

I’ve looked and looked but can’t seem to figure out if this can be done. But, I have an applescript that runs on a regular basis to “scan for new files” in one of my libraries. It works well. However, I keep closing the library when in reality I want to keep it open. So, I’d like to include in my applescript scan for new files, a statement that would simply look and see if the specified library is open and if it’s not open then to have applescript open it and then exit the applescript. But if the specified library is already open then to skip down to the “scan for new files” line.

Can this be done?

Art

Maybe something like this will get you going. The continuation character “¬” is produced
by typing option-L (lower case)

tell application "EagleFiler"
if not (exists library document "Personal.eflibrary") then
tell application "Finder"
open file "Personal.eflibrary" of folder "Personal" of folder "EagleFiler Docs"¬
of folder "Documents" of folder "ajoyce" of folder "Users" of startup disk
end tell
end if
end tell

Great! Thanks! I made a slight adjustment and got the following that works exactly the way that I wanted it to work. Thank you!
Here’s my final coding:


tell application "EagleFiler"
	if not (exists library document "Data Files.eflibrary") then
		tell application "Finder"
			open file "Data Files.eflibrary" of folder "Data Library" of folder ¬
				"Libraries-EagleFiler" of folder "art" of folder "Users" of startup disk
		end tell
	else
		tell library document "Data Files.eflibrary"
			scan for new files
		end tell
	end if
end tell

I’m glad it works. Three points:

  1. I just noticed the link to Michael’s script http://c-command.com/forums/showthread.php/2164-Open-Library-with-Script, which is simpler and avoids the call to Finder.
  2. Your “else” clause could prevent the scan of the just-opened library.
  3. EagleFiler might not have finished opening the library before the “scan” command is given.
    Combining Michael’s code with your scan request, something like:

tell application "EagleFiler"
if not (exists library document "Personal.eflibrary") then
open POSIX file "/Users/Art/Documents/EagleFiler Docs/Personal/Personal.eflibrary"
end if
delay 1
tell library document "Personal.eflibrary" to scan for new files
end tell

Thanks for the comments. Now I have a couple to make in response.

  1. Thank you for directing me to the other script. I’m not sure that I like it, however, as much as I like what I made.
  2. The “else” clause does not prevent the scan of the just-opened library because whenever a library opens it automatically does the scan.
  3. You’re right about EF not having finished opening the library before the “scan” command is given. I tried the script without the “else” clause and got an error because the library wasn’t open enough for the scan command to have effect. So I considered putting in a pause. But whenever I’ve tried pausing in a script I’ve found that it’s not as precise because on any given day, my computer may be slower or faster in doing what it’s doing. So, that pause needs to be set to be sufficiently long enough to cover any of the variables in time. Since the library, after it opens, does an automatic scan, I didn’t need to tell it to do a scan. But if the library was already open, then I wanted it to do the scan. And the code as I listed it, does exactly that . . . and does it beautifully, I might add.
  4. I am interested to know what is meant by the code in the other script where it says, "open POSIX file “/Users. . .” What does POSIX mean?

Again, I thank you for the help. I thought there might be a better way to put the path to the file other than saying,
“open file “Data Files.eflibrary” of folder “Data Library” of folder “Libraries-EagleFiler” of folder “art” of folder “Users” of startup disk” - I don’t know that I’ve seen a path defined in that direction before, but it worked and why tamper with success.

Art

I think this is actually OK because EagleFiler does its own automatic scan when opening a library.

Yes, it’s best not to rely on pauses in scripts. I’d be interested to hear your results, but I think the pause might not be needed if you write the script my way, e.g.:

tell application "EagleFiler"
    open POSIX file "/Users/mjt/Desktop/EagleFilerTest/EagleFilerTest.eflibrary"
end tell

When you tell the Finder to open the file, I think the Finder considers the command to be done as soon as it’s handed off the file to whichever application will open it. Whereas, if you tell EagleFiler to open the file, AppleScript should know to wait until EagleFiler has actually finished doing so.

POSIX is standardized form of Unix. In AppleScript, “POSIX file” is a way of specifying a file using a Unix-style path. (If you just say “file” it uses the class Mac-style paths with colons instead of slashes.) These are the common ways of referring to files in AppleScript, because they work in all apps; terms like “folder” and “startup disk” are only valid when talking to the Finder.

Got it. I don’t Scan for New Files myself anymore, so I’ve forgotten that it can be the default behavior after opening a library.

I wasn’t scanning for new files, but needed to do so because I would put something in the library and then go to find it and couldn’t because it hadn’t yet been recognized by EF. And since I try to leave the library open all the time, I needed to have something that would automatically from time to time scan for new files. Hence the applescript.

I tried putting in the code:

tell application "EagleFiler"
	if not (exists library document "Data Files.eflibrary") then
		open POSIX file "/Users/art/Libraries-EagleFiler/Data Library/Data Files.eflibrary"
	else
		tell library document "Data Files.eflibrary"
			scan for new files
		end tell
	end if
end tell

But it kept getting a error message that said “missing value” when I would run the script. The Replies button revealed the following:

tell application "EagleFiler"
	exists library document "Data Files.eflibrary"
		--> false
	open file "Voyager:Users:art:Libraries-EagleFiler:Data Library:Data Files.eflibrary"
		--> missing value
end tell
Result:
missing value

I don’t know what the missing value is, but this just won’t run. And I thought it interesting that I had “open POSTIX file . . .” but the result shows a simple "open file. . . " and changed the “/” to “:”

I think the original that I had works best of anything I’ve tried thus far, even though it’s going to the Finder and I tried eliminating that but kept getting errors. And that original was:

tell application "EagleFiler"
	if not (exists library document "Data Files.eflibrary") then
		tell application "Finder"
			open file "Data Files.eflibrary" of folder "Data Library" of folder ¬
				"Libraries-EagleFiler" of folder "art" of folder "Users" of startup disk
		end tell
	else
		tell library document "Data Files.eflibrary"
			scan for new files
		end tell
	end if
end tell

It’s OK for it to say “missing value”. The “open” command doesn’t return a value; it just opens the library.

That’s normal; AppleScript canonicalizes to the old-style paths.