【问题标题】:Automate file uploading with AppleScript and Safari使用 AppleScript 和 Safari 自动上传文件
【发布时间】:2015-12-21 11:05:21
【问题描述】:

我正在尝试自动化部分工作流程,其中涉及通过网页上传一系列图像,其中 AppleScript 会在已加载的页面中调用 JavaScript。

我以前做过这种自动化,但在实际选择要上传的文件时遇到了一个主要障碍。我无法设置input.value 属性(出于安全原因,这是不允许的),我什至无法弄清楚如何让页面放置文件选择器,然后我希望可以填充它。该页面使用 Angular,以防有助于提出解决方案。

这是页面的 HTML 的样子(相关部分):

<div class="...">
    <div class="..." ng-class="...">
        <span class="... ng-binding" ng-bind-html="...">Choose File</span>
        <div class="...">
            <input id="..." type="file" ng-file-select="onFileSelect($files)" multiple="">
        </div>
    </div>
</div>

如何使用加载到页面 DOM 中的 JavaScript 模拟单击​​“选择文件”?

【问题讨论】:

    标签: javascript html angularjs automation applescript


    【解决方案1】:

    这基本上就是你所说的。对我来说最绝望的部分是 javascript 部分,但幸运的是我在一个名为 cubemg 的网站上找到了一些预制的 applescript javascript 函数。

    to clickClassName(theClassName, elementnum)
        tell application "Safari"
            do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
        end tell
    end clickClassName
    
    to getValueFromClass(theclass, num)
        tell application "Safari"
            tell document 1
                set theFirstTableHTML to do JavaScript "\n document.getElementsByClassName('" & theclass & "')[" & num & "].value"
                return theFirstTableHTML
            end tell
        end tell
    end getValueFromClass
    
    
    on run
        choose file with prompt "Which folder would like average file size calculated?"
        open {result}
    end run
    
    on open theimage
        --tell application "Finder" to set xx to every file in item 1 of theimage
        --display dialog "Hey! the file's alias is: " & (path of theimage as string)
        --display dialog theimage
        set filepath to POSIX path of theimage
    
        tell application "Safari" to open location "https://upload.vstanced.com"
        delay 2
        clickClassName("btn btn-big white outline", 0)
        tell application "System Events"
            activate application "Safari"
            delay 0.5
            keystroke "g" using {shift down, command down} --open goto
            set the clipboard to filepath
            keystroke "v" using {command down}
            delay 0.7
            keystroke return -- enter goto text
            delay 0.4
            keystroke return --press enter on file
    
        end tell
        delay 1
        clickClassName("btn btn-big green", 0)
    
        set thedirectlink to ""
        repeat 15 times
            set thedirectlink to getValueFromClass("r2", 1)
            delay 1
            if thedirectlink is not equal to "" then
                exit repeat
            end if
        end repeat
        set the clipboard to thedirectlink
        tell application "Safari" to close current tab of window 1
    
        display notification "Upload complete" with title "VStanced Upload" subtitle thedirectlink sound name "blow"
    end open
    

    如果您想查看此内容,请在脚本编辑器中将其保存为“应用程序”文件格式。然后确保 safari 已打开(或延长延迟)并通过将图像拖到脚本上来使用它(您可以将其放在您的码头或桌面上)。完成后,它会执行以下操作

    1. 打开新标签
    2. 转到图片托管网站
    3. 打开“文件选择子窗口”
    4. 打开 goto 东西
    5. 粘贴路径
    6. 两次回车(首先是转到,然后是选择文件窗口本身)
    7. 等待直接链接加载,将其复制到剪贴板,关闭选项卡,然后“运行”有声通知

    即使使用预制功能,我也很难找到有效的类/ID。

    这可能很大程度上是因为我花了很长时间试图查看是否可以使用 applescript 和 javascript 的某种组合以某种方式将文件粘贴或拖放到浏览器中。我仍然认为必须有一种方法可以避免做这么多按键操作和使用这么多延迟。即使有一种“拖放”的方法也会有很大帮助。

    此外,当您单击按钮打开选择文件窗口时,页面不会刷新,所以我觉得 html 中有一些字段可以使用 javascript 设置为文件路径。我只是不知道它叫什么。

    如果您发现或找到了另一种打开“文件选择窗口”的方法,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-16
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多