【问题标题】:Avoid AppleScript to make line breaks避免使用 AppleScript 进行换行
【发布时间】:2014-07-15 08:38:17
【问题描述】:

我正在使用这个 AppleScript 来获取所选电子邮件的内容:

using terms from application "Mail"
    on run {input, parameters}
        set mailContents to {}
        repeat with aMessage in input
            set end of mailContents to content of aMessage
        end repeat
        return mailContents
    end run
end using terms from

结果如下:

{"
  
Here's some text followed by a longer URL, which is cut by a line break!  

http://www.XYZ.net/diario/actualidad/economia/20140714/-ciudad-bar
ata-para-el-turismo_353_34541.html

"}

我想使用这些特定邮件中的所有链接进行处理,但不能使用前面示例中看到的分割 URL。 那么如何告诉 AppleScript 将 URL 保持在一起呢?

【问题讨论】:

  • 如果每个 URL 链接都被分成两段,那么就在您的重复循环中,搜索 aMessage 中以“http:”开头的行并删除它末尾的段落标记。
  • 谢谢@jweaks!只是对一行很长的 URL 会被拆分。知道如何删除段落标记吗?我尝试了十几种不同的想法,但无法实现。 ;(
  • 嗯,使用 do shell script grep 行匹配行并删除段落标记会很容易。

标签: applescript automator


【解决方案1】:

试试这个。我从您的 mailContents 示例开始。您最终会得到 httpLinks 变量中的链接列表。假设每个链接都以“http”开头并以“html”结尾,则此方法有效。祝你好运。

set mailContents to {"
  
Here's some text followed by a longer URL, which is cut by a line break!  

http://www.XYZ.net/diario/actualidad/economia/20140714/-ciudad-bar
ata-para-el-turismo_353_34541.html

"}

set httpLinks to {}
repeat with i from 1 to count of mailContents
    set thisContent to item i of mailContents

    -- remove all return characters (mac, unix, and windows characters)
    set AppleScript's text item delimiters to {character id 10, character id 13, character id 13 & character id 10}
    set textItems to text items of thisContent
    set AppleScript's text item delimiters to ""
    set newText to textItems as text

    -- find links
    set AppleScript's text item delimiters to "http"
    set textItems to text items of newText
    if (count of textItems) is greater than 1 then
        set AppleScript's text item delimiters to "html"
        repeat with j from 2 to count of textItems
            set linkItems to text items of (item j of textItems)
            set thisLink to "http" & item 1 of linkItems & "html"
            set end of httpLinks to thisLink
        end repeat
    end if
    set AppleScript's text item delimiters to ""
end repeat

return httpLinks

【讨论】:

  • 非常感谢!我不得不稍微定制你的脚本,因为不是每个 URL 都以 html 结尾。此外,我使用消息的source 而不是content,因为删除的换行符会导致很难将 URL 与其他文本分开。他们之间没有空隙。你可以看看我现在使用的代码!再次感谢您!
【解决方案2】:

这不会断行,希望你能以某种方式使用它:

tell application "Mail"
  set mailSelection to selection
  set mailContents to {}
  repeat with mail in mailSelection
    set end of mailContents to content of mail
  end repeat
  get mailContents
end tell

【讨论】:

    【解决方案3】:

    非常感谢!我不得不稍微定制你的脚本,因为不是每个 URL 都以 html 结尾。此外,我使用消息的source 而不是content,因为删除的换行符会导致很难将 URL 与其他文本分开。他们之间没有空隙。这是我的实际代码: `使用应用程序“邮件”中的术语 在运行{输入,参数} 将 mailContents 设置为 {} 在输入中重复 aMessage 将 mailContents 的结尾设置为 aMessage 的源 结束重复

        set httpLinks to {}
        repeat with i from 1 to count of mailContents
            set thisContent to item i of mailContents
    
            -- remove all return characters (mac, unix, and windows characters)
            set AppleScript's text item delimiters to {"=09", "=" & character id 10, "=" & character id 13, "3D", character id 92}
            set textItems to text items of thisContent
            set AppleScript's text item delimiters to ""
            set newText to textItems as rich text
    
            -- find links
            set AppleScript's text item delimiters to "http"
            set textItems to text items of newText
            if (count of textItems) is greater than 1 then
                set AppleScript's text item delimiters to "target"
                repeat with j from 2 to count of textItems
                    set linkItems to text items of (item j of textItems)
                    set thisLink to "http" & item 1 of linkItems & "target"
                    set end of httpLinks to thisLink
                end repeat
            end if
            set AppleScript's text item delimiters to ""
    
        end repeat
    
        set list1 to httpLinks
        set list2 to {}
        repeat with x from 1 to count of items of list1
    
    
            set n to item x of list1
            if n is not in list2 then set end of list2 to n
        end repeat
    
        return list2
    
    
    
    end run
    

    使用来自`的术语结束

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      相关资源
      最近更新 更多