【问题标题】:MacOS Terminal wait for opened programMacOS终端等待打开的程序
【发布时间】:2021-01-29 16:19:24
【问题描述】:

我使用名为 PDF Lightweight 的应用程序来压缩我的 PDF(因为我的体验非常好),并使用 Thunderbird 来发送电子邮件。

我想编写一个 AppleScript,在将 PDF 附加到电子邮件之前对其进行压缩:

set attachment1 to "/Users/username/Desktop/Test.pdf"

do shell script "open -a " & quoted form of ("/Applications/Lightweight PDF.app") & " " & quoted form of attachment1

set email_attachment to "attachment=" & "'file://" & attachment1 & "'"
set thunderbird_bin to "/Applications/Thunderbird.app/Contents/MacOS/thunderbird-bin -compose "
set arguments to email_attachment

do shell script thunderbird_bin & quoted form of arguments & ">/dev/null 2>&1 &"

问题是原始 PDF 将在压缩完成之前附加。我尝试使用this 之类的东西,但它也不起作用。我想是因为我使用“打开”命令启动 PDF Lightweight,即使应用程序仍在压缩,该命令在技术上也是完整的。

知道如何让脚本等待 PDF 压缩吗?

谢谢!

【问题讨论】:

    标签: terminal applescript


    【解决方案1】:

    我现在通过查看文件大小来使用解决方法。一旦工作完成,pdf应该更小,所以是时候继续前进了。

    set oldSize to text item 1 of (do shell script "du " & quoted form of ShrinkPDF)
    set newSize to text item 1 of (do shell script "du " & quoted form of ShrinkPDF)
    
    do shell script "open -a " & quoted form of ("/Applications/Lightweight PDF.app") & " " & quoted form of ShrinkPDF
    
    set timer to 0
    
    repeat while newSize = oldSize and timer < 6
        set newSize to text item 1 of (do shell script "du " & quoted form of ShrinkSize)
        delay 1
        set timer to timer + 1
    end repeat
    
    if newSize = oldSize then
        tell application "Lightweight PDF"
            activate
        end tell
        display dialog "This is taking a while maybe you want to quit manually..."
    end if
    
    tell application "Lightweight PDF"
    quit
    end tell
    

    【讨论】:

      【解决方案2】:

      知道如何让脚本等待 PDF 压缩吗?

      在使用 AppleScript 时,您确实不需要使用 do shell script command 来打开 PDF Lightweight 并处理 PDF 文件

      以下示例 AppleScript 代码将打开PDF Lightweight并处理目标PDF file 并且在完成处理 file 之前不会继续处理剩余的 code,再加上几秒钟:

      set thisPDF to "/path/to/filename.pdf"
      
      tell application id "com.enoughpepper.lightweightpdf"
          open thisPDF's quoted form
          quit
      end tell
      
      --  # Code placed here will not run until the 
      --  # PDF file has finished being processed.
      

      如果出于某种原因您想使用do shell script 命令 打开 PDF Lightweight 并处理 PDF 文件。

      以下示例 AppleScript 代码将打开PDF Lightweight并处理目标PDF file 并且在完成处理 file 之前不会继续处理剩余的 code

      set thisPDF to "/path/to/filename.pdf"
      
      set shellCmd to ¬
          "ps -eo  %cpu,command | awk '/[L]ightweight PDF/{print int($1)}'"
      
      do shell script ¬
          "open -j -b 'com.enoughpepper.lightweightpdf'" & ¬
          space & thisPDF's quoted form
      
      delay 2
      repeat while (do shell script shellCmd) is greater than 0
          delay 1
      end repeat
      
      tell application id "com.enoughpepper.lightweightpdf" to quit
      
      --  # Code placed here will not run until the 
      --  # PDF file has finished being processed.
      

      在这种示例 AppleScript 代码的形式中,它确定应用程序已完成处理 PDF 文件,因为它不再消耗 CPU 周期。换句话说,当 file 处理完成时,do shell script command 返回 0。

      【讨论】:

      • 非常感谢!我尝试了您的第一种方法,但在我看来,“thisPDF 的打开引用形式”似乎找不到文档路径,而“打开 POSIX 文件附件1”确实找到了文件但没有压缩它。 (而且我知道可以压缩它,因为当我将 filr 拖放到 Lightweight 时它可以工作)
      猜你喜欢
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 2016-07-14
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多