2012-03-06 67 views
2

我想写一个苹果脚本来切换Microsoft Word中的超链接。 (这通常通过按Alt + F9完成)。苹果脚本切换超链接

这里是我的剧本,但它不工作:

tell application "Microsoft Word" 
     keystroke F9 using {option down} 
end tell 

这给了我一个错误:

"Expected end of line but found identifier"

如果我使用:

tell application "System Events" 
    tell application "Microsoft Word" 
     keystroke F9 using {option down} 
    end tell 
end tell 

它的工作原理,但什么也没做。

如果我使用:

tell application "System Events" 
    tell application "Microsoft Word" 
     keystroke "Hello" 
    end tell 
end tell 

它只是打印 “Hello” 的AppleScript的窗口。我需要它来影响MS词。

回答

1

无需在此处编写模拟键盘快捷方式。切换字段代码(例如形式{HYPERLINK "http://www.stackoverflow.com"}和实际的超链接之间),使用此脚本:

# toggle field codes 
# same as option + F9 
# tested in Microsoft® Word 2008 for Mac v 12.2.3 
tell application "Microsoft Word" 
    set theView to active window's active pane's view 
    if (show field codes of theView) then 
     set show field codes of theView to false 
    else 
     set show field codes of theView to true 
    end if 
end tell 

注意,这也将切换其他域代码,如页码,关闭和打开。

+0

太棒了!完美的作品。谢谢。 – solerous 2012-03-07 15:21:46