9 Replies Latest reply on Jan 16, 2015 5:18 PM by steve_ssh

    How to get the document of the background window with AppleScript ?

    Vincent_L

      Hi,

       

      I need to get the "document" (= the filename), of the background window just below the current one, in AppleScript.

       

      I can get the background window name, but I want the name of the file (because it can differ rom the window name).

       

      tell application "FileMaker Pro Advanced"

        name of window 2

      end tell

       

      I can also get the base table of that window, but I still can't get the document


      tell application "FileMaker Pro Advanced"

        set basetable to name of current table of window 2

      end tell

       

      However I can get the specifier of the base table, and that reference contains the document, but I can't access it

       

      tell application "FileMaker Pro Advanced"

        set a to current table of window 2 as specifier

      end tell

      returns : table "Attributes" of document "Contacts.fmp12" of application "FileMaker Pro Advanced"

       

      "document of window 2" doesn't work

       

      Filemaker's AppleScript dictionary says that window is contained by document. So is there a way to get the container of an AppleScript object

       

      "container of window 2" doesn't work.

       

      I've been pulling my hairs for 2 days, I'm going nuts.

       

      Thanks for your help

       

      P.S : If I can get the behind's window database file, using Filemaker scripting, I'm ok with that. But I tried, and I think that's no possible reliably (regardless window names)

        • 1. Re: How to get the document of the background window with AppleScript ?
          BowdenData

          Wouldn't the FM function Get ( FileName ) be what you are looking for? It returns the actual disk filename. See link FM Help page.

           

          Doug

           

          https://fmhelp.filemaker.com/fmphelp_13/en/html/func_ref2.32.36.html#1029337

          • 2. Re: How to get the document of the background window with AppleScript ?
            Vincent_L

            No, I need to get the database of the BACKGROUND window.

            all filemaker Get function applies to the active = frontmost window.

             

            And I can't modify the solution (I wan't it to work with all filemaker files without modifying them).

            • 3. Re: How to get the document of the background window with AppleScript ?
              APIcoder

              If you have 2 databases open, databases "db_2" and databases "db_1",

              the name of window 1  of database "db_1" is db_1

              the name of window 1  of database "db_2" is db_2

               

              Open a new window of database "db_2"  ( menu Window --> new window )

              the name of window 2  of database "db_2"  -- "db_2 - 2"

               

              if you want the name of a window of database

              tell application "FileMaker Pro Advanced"

                 set a to name of database 1 -- db_2.fmp12

                 set b to name of database 2 -- db_1.fmp12

               

                  set a to name of window 1 of database 2

                  set b to name of window 2 of database 2

               

                 set a to name of window 1 of database "db_2"

                 set b to name of window 2 of database "db_2"

              end tell


              all window names of a database

              tell application "FileMaker Pro Advanced"

                set a to  name of every window  of database 2

               

                set a to name of every window of database "db_1.fmp12"

              end tell


              Open a new window of database "db_1.fmp12"

              script stap: new window [ name is "TEST" ] )  -- new window opens on top

              tell application "FileMaker Pro Advanced"

                set a to  name of  window 1  of database "db_1.fmp12" -- "TEST"

              end tell


              • 4. Re: How to get the document of the background window with AppleScript ?
                Vincent_L

                Thans for the effort APIcoder, but :

                 

                - You assume that the window name = the database name. Thought its mostly the case, it's not guaranteed (you can have scripts that have renamed the window).

                 

                - Database 2 is unfortunately not the background database, the index of the databases is, I think the order of their opening, and even if they're hidden.

                 

                Also I don't know the name of the databases before hand, I though about listing all the db, and get their windows, but for some reason (perhaps bad syntax from me), I can't get the index of their windows.

                 

                What I need is to get a, robust, reliable, link between te database name and the window index.

                 

                I have a solution A with many files open, I want to open a file, which is my new utility solution that will get the database of the window of A under it. My utility file can't rely on anithing from A, and I can't modify A (becausse it could be solution B, C,D...Z)

                • 5. Re: How to get the document of the background window with AppleScript ?
                  steve_ssh

                  Hello Vincent,

                   

                  As a last resort, have you tried a brute force way of iterating through all window objects of every document until you find the one with the desired index?

                   

                  I'll paste some sample code for the idea below, but please take it with a grain of salt, as I don't have strongly developed AppleScript chops.

                   

                  The most obvious problem that I see with this code is that it has to iterate through nearly *everything* in order to find its result.

                   

                  I would think that there should be some means to directly get at the desired result, but I do not know what it is.

                   

                  HTH & Best regards,

                   

                  -steve

                   

                   

                  set varDocument to getTargetFmDocument(2)

                   

                  tell application "FileMaker Pro Advanced"

                    set cell "foo" of record 1 of varDocument to "hello world..."

                  end tell

                   

                   

                   

                  on getTargetFmDocument(TargetWindowIndex)


                    tell application "FileMaker Pro Advanced"

                        set varDocuments to get every document

                        set varDocumentArrayIndex to 1

                   

                        repeat until varDocumentArrayIndex > number of items in varDocuments

                            set varDocument to item varDocumentArrayIndex of varDocuments

                            set varWindows to every window of varDocument

                            set varWindowArrayIndex to 1

                   

                            repeat until varWindowArrayIndex > number of items in varWindows

                                set varWindow to item varWindowArrayIndex of varWindows

                   

                                if (index of varWindow = TargetWindowIndex) then

                                    return varDocument

                                end if

                   

                                set varWindowArrayIndex to varWindowArrayIndex + 1

                            end repeat

                   

                            set varDocumentArrayIndex to varDocumentArrayIndex + 1

                        end repeat

                   

                    return null

                    end tell

                  end getTargetFmDocument

                  • 6. Re: How to get the document of the background window with AppleScript ?
                    APIcoder

                    Maybe this works for you.

                     

                    You can run a Filemaker Script from AppleScript

                     

                    Filemaker Script: "getName"

                    #---[  set database name and window name to Field script_result in table script_table

                    Set field [script_table::script_result , Get ( WindowName ) & ¶ & Get ( FileName ) ]

                    Commit[]

                     

                    set myResult to getName() of me

                    display dialog myResult as string giving up after 1

                     

                    on getName()

                      tell application "FileMaker Pro Advanced"

                      go to window 2  

                                do script "getName"

                                delay 1

                                set myResult to contents of field "script_result"

                                delay 1

                                set field "script_result" to ""

                                return myResult

                      end tell

                    end getName

                    • 7. Re: How to get the document of the background window with AppleScript ?
                      Vincent_L

                      Thanks steve I thought about it, but was reluctant about it cause it's not elegant :-)

                      But your post encouraged me to go brute force.

                       

                      But there should be a one line way, something like that

                       

                      every document whose wndow's index is 2   (but this doesn't work)

                       

                      Here it is in simplest form (less elegant than yours)

                       

                      tell application "FileMaker Pro Advanced"

                        set myDoc to ""

                       

                        repeat with i in every document

                       

                                  repeat with j in every window of i

                       

                                      if (index of j) = 2 then

                                            set myDoc to name of i

                                            exit repeat

                                      end if

                       

                                  end repeat

                       

                            if myDoc is not "" then exit repeat

                       

                        end repeat


                      end tell

                      myDoc

                      • 8. Re: How to get the document of the background window with AppleScript ?
                        Vincent_L

                        Hi APiCoder,

                         

                        Apperciated, but unfortunately, if you use a filemaker script, that script has to be stored in the database file that you want to discover. So this will need to modify the target solution and inject the script "getName"

                        • 9. Re: How to get the document of the background window with AppleScript ?
                          steve_ssh

                          Vincent_L wrote:

                           

                          But there should be a one line way, something like that

                           

                          every document whose wndow's index is 2  (but this doesn't work)

                          I totally agree -- there should at least be some way to look up the container hierarchy from a child to a parent.  There may be such a way, but until we discover it, at least we have our inelegant means. 

                           

                          I like your code better than what I posted.  I wasn't familiar with using the the [repeat with/in] construct in AppleScript -- I'm glad you posted it so that I could see that and learn it.

                           

                          Thanks for your reply and cheers for building your own utilities.  I don't know whether you do it strictly for personal use or for commercial distribution, but, either way it's great fun to build such solutions.

                           

                          Very best,

                           

                          -steve