复制当前选择&网址并粘贴到Excel?

时间:2013-11-23 14:51:41

标签: automation applescript

我想要制作的剧本必须:

  1. 复制Safari中的当前选择和网址
  2. 在cell1中选择粘贴
  3. 在cell2中粘贴网址
  4. 转到Excel中cell1下面的新行
  5. 如何使用AppleScript完成此操作?

    我从这开始:

    tell application "Safari"
        set theURL to URL of front document
        set theText to (do JavaScript "(''+getSelection())" in document 1)
    end tell
    
    
    tell application "Microsoft Excel"
        set value of active cell to the theURL
    end tell
    

    但我不知道如何使用AppleScript 导航到相邻的单元格。

    adamh 的代码完美地做到了这一点:

    tell application "Safari"
        set theURL to URL of front document
        set theText to (do JavaScript "(''+getSelection())" in document 1)
    end tell
    
    tell application "Microsoft Excel"
        set foundRange to find range "A:A" what "" -- finds the first empty cell in column A
        set the value of foundRange to theText      
        -- Work out its adjacent cell in same row
        set cur_col to the first column index of foundRange
        set cur_row to the first row index of foundRange
        set nextCell to cell cur_row of column (cur_col + 1)
        set value of nextCell to theURL
        select cell (cur_row + 1) of column cur_col -- select the next logical cell
    end tell
    

1 个答案:

答案 0 :(得分:2)

根据您的上述代码提供此内容。

它的工作原理是在A列中找到插入点的第一个空单元格。

tell application "Safari"
    set theURL to URL of front document
    set theText to (do JavaScript "(''+getSelection())" in document 1)
end tell

tell application "Microsoft Excel"
    set foundRange to find range "A:A" what "" -- finds the first empty cell in column A
    set the value of foundRange to theText      
    -- Work out its adjacent cell in same row
    set cur_col to the first column index of foundRange
    set cur_row to the first row index of foundRange
    set nextCell to cell cur_row of column (cur_col + 1)
    set value of nextCell to theURL
    select cell (cur_row + 1) of column cur_col -- select the next logical cell
end tell