AppleScript: How to refer to a file opened by the same script

Discussion in 'Mac Programming' started by yux, Sep 14, 2011.

  1. yux macrumors newbie

    Joined:
    Sep 9, 2011
    #1
    I am trying to write an applescript to open a Numbers file and perform some tasks with the file, say display the row count of the file. Here is what I wrote:

    tell application "Numbers"
    tell application "Finder"
    set theFile to file "myfolder:myfile.numbers"
    open theFile
    end tell
    -- the above part works well
    tell document theFile to tell sheet 1 to tell table 1
    set z to row count
    end tell
    end tell
    display dialog ("The row count is" & z)

    The open file part works well, but I cannot get the opened file referred to, no matter what. Could anyone teach me how to solve this problem? Thanks!
     
  2. hayesk macrumors 65816

    Joined:
    May 20, 2003
    #2
    Refer to the document by name, not the path

    E.g. tell document "my file"
    Or tell document whose name is "my file"
    Or something like that.
     
  3. yux thread starter macrumors newbie

    Joined:
    Sep 9, 2011
    #3
    AppleScript: How to refer to a file opened by the same script

    Thanks for getting back to me this fast! I changed the script as follows, but it still does not work. The error message says "Can't get document whose name = "myfile.numbers".

    tell application "Numbers"
    tell application "Finder"
    set theFile to file "myfolder:myfile.numbers"
    open theFile
    end tell
    -- the above part works well
    tell (document whose name is "myfile.numbers") to tell sheet 1 to tell table 1
    set z to row count
    end tell
    end tell
    display dialog ("row count is" & z)
     
  4. yux thread starter macrumors newbie

    Joined:
    Sep 9, 2011
    #4
    If I open the file manually and run the following script, or substitute the document name with document 1, it works well:

    tell application "Numbers"
    tell document "myfile.numbers" to tell sheet 1 to tell table 1
    set z to row count
    end tell
    end tell
    display dialog ("The row count is " & z)

    It seems the script just can't recognize a file opened by itself.
     
  5. yux thread starter macrumors newbie

    Joined:
    Sep 9, 2011

Share This Page