Applescript Force MS Word to open URL

Discussion in 'Mac Programming' started by ip.kban, Aug 26, 2014.

  1. ip.kban macrumors newbie

    Dec 6, 2013
    Hi all
    I need to have a script to simulate Microsoft's Word "File->Open URL" command and paste my file's URL there.
    I need to open my file from server, without downloading, using WebDav.

    I've tried to use following script:
    tell application "/Applications/Microsoft Office 2011/Microsoft Word"
    open file
    end tell
    but i got error:
    "execution error: Microsoft Word got an error: file doesn’t understand the open message. (-1708)"

    Thank you for any help.
  2. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK
    Hi there,

    I don't see anything particularly useful in the Microsoft Work dictionary, although it's lengthy and (in my opinion) a bit counter intuitive so I may have missed it.

    Are you able to mount the WebDav volume on your desktop, as you would a share from a server? If so, could you just mount the volume and open it as a normal file from there?
  3. ip.kban thread starter macrumors newbie

    Dec 6, 2013
    Thanks, but it's not my case.
    Anyway, i changed my script, so it's working now.

    I have used this topic:

    Here is te result:

    on menu_click(mList)
        local appName, topMenu, r
        -- Validate our input
        if mList's length < 3 then error "Menu list is not long enough"
        -- Set these variables for clarity and brevity later on
        set {appName, topMenu} to (items 1 through 2 of mList)
        set r to (items 3 through (mList's length) of mList)
        -- This overly-long line calls the menu_recurse function with
        -- two arguments: r, and a reference to the top-level menu
        tell app "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
            (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
    end menu_click
    on menu_click_recurse(mList, parentObject)
        local f, r
        -- `f` = first item, `r` = rest of items
        set f to item 1 of mList
        if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
        -- either actually click the menu item, or recurse again
        tell app "System Events"
            if mList's length is 1 then
                click parentObject's menu item f
                my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
            end if
        end tell
    end menu_click_recurse
    tell application "Microsoft Word" to activate
    menu_click({"Microsoft Word", "File", "Open URL..."})
    tell application "System Events"
    	tell process "Microsoft Word"
    		tell window "Open URL"
    			set value of combo box 1 to ""
    		end tell
    		click button "Open" of window "Open URL"
    	end tell
    end tell
    May be it'll be helpful for someone.
  4. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK
    Good work! I have a (poooooooossibly irrational) fear and loathing of UI Scripting, but if it gets the job done for you then go for it.
  5. snorkelman, Aug 26, 2014
    Last edited: Aug 26, 2014

    snorkelman macrumors 6502a


    Oct 25, 2010
    tell application "Microsoft Word"
    	open ""
    end tell
    seems to do same as choosing file "open url" from menu?

    issue with your original script was that you had 'open file' as the command rather than just 'open' if I include the word file in my script it would generate same error message/code
  6. ip.kban thread starter macrumors newbie

    Dec 6, 2013
    snorkelman, thank you, that's exactly what i need!
    But here's another problem. I'm trying to execute my appplescript from c++ code (i'w writing an firebreath plugin). When i'm executing it directly from terminal, it's ok, but when i'm executing it from c++, using QT framework, i'm getting error "syntax error: A unknown token can’t go here. (-2740)".
    Here is the code:
    cmd = QString("osascript -e 'tell application \"Microsoft Word\" to open \"%1\"'").arg(uri);
    ret = QProcess::startDetached(cmd);
    Why it can happens? What's wrong with syntax?
  7. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Apply some basic debugging:
    cmd = QString("osascript -e 'tell application \"Microsoft Word\" to open \"%1\"'").arg(uri);
    [COLOR="Red"]// print the cmd string here[/COLOR]
    ret = QProcess::startDetached(cmd);
    You can use whatever function or method you want to print the cmd string, but it must accurately represent the exact contents of the string.

    When the cmd string is printed, post it so we can see what the failing command actually is. To ensure accuracy, copy and paste the text into a post; don't retype it.

    You're asking us to find the syntax error in a command that no one has seen in its complete expanded form. There could be any number of reasons for the failure, such as unescaped quotes, unescaped backslashes, invalid URI characters, etc. Those are just a few possibilities, which is why we need to see the exact command to be executed.
  8. ip.kban thread starter macrumors newbie

    Dec 6, 2013
    chown33, thank you for your answer. But the command is ok, i've used console print to get command result, and then copied it into terminal, and it's worked fine. I think it's something with QProcess::startDetached function, its strange, because all other terminal functions, like "open -a" worked fine in it. Anyway, i've changed my code, now i'm using NSAppleScript for running the script, and it's ok now.
    Thank you all for your answers.

Share This Page