首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过Automator运行Javascript排序

通过Automator运行Javascript排序
EN

Stack Overflow用户
提问于 2015-08-02 16:19:37
回答 3查看 2K关注 0票数 4

我试图在Apple Automator中使用以下代码

代码语言:javascript
复制
var lnRange = getSelectedLineRange();
var ln = getTextInRange(lnRange[0],lnRange[1]);

var lines = ln.split('\n').sort(function(a, b)
{
  var parsedA = a.replace(/\*\s(<s>)?(The )?/, "* ");
  var parsedB = b.replace(/\*\s(<s>)?(The )?/, "* ");

  return parsedA.localeCompare(parsedB);
});
setTextInRange(lnRange[0],lnRange[1],lines.join('\n'));

我知道代码是健全的,并实现了我需要的结果(在iPhone上的草稿中运行它会产生所需的结果,即在行的开头忽略<s>和/或"The“的使用),即对标记列表进行排序。*需要待在列表中,这样列表才能有效)。

将它从iPhone转移到Automator是事情分崩离析的地方,因为Automator找不到变量getSelectedLineRange。我猜这是Automator处理文本输入的方式和脚本想要接收和处理文本输入之间的冲突,但是我在如何解决它方面陷入了僵局。

为了举例(如果我的整个方法都是错误的),我想要这个列表,在任何文本字段中我都可以抛出它。

代码语言:javascript
复制
* Armadillo
* The aardvark
* <s>Rhino</s>
* <s>The Zebra</s>
* The Giraffe
* Hedgehog

当被选中时,要遍历脚本,以服务的形式运行,然后如下所示

代码语言:javascript
复制
* The aardvark
* Armadillo
* The Giraffe
* Hedgehog
* <s>Rhino</s>
* <s>The Zebra</s>

我当然没有使用javascript解决方案,但这是我的出发点。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-13 12:07:46

"Run JavaScript“操作在Automator中需要run(input, parameters)函数。

输入是包含一个项(文本)的数组。

代码语言:javascript
复制
function run(input, parameters) {
   var lines = input[0].split('\n').sort(function(a, b)
   {
      var parsedA = a.replace(/\*\s(<s>)?(The )?/, "* ");
      var parsedB = b.replace(/\*\s(<s>)?(The )?/, "* ");
      return parsedA.localeCompare(parsedB);
   });
   return lines.join('\n');
}
票数 2
EN

Stack Overflow用户

发布于 2015-08-03 04:57:00

我对JavaScript还不太熟悉,无法帮助解决这个问题,但是这个AppleScript应该可以在Run AppleScript Action中工作:

代码语言:javascript
复制
on run {input}

    set rawParagraphs to every paragraph of (input as text)

    set recordsForSorting to {}

    repeat with eachPara in rawParagraphs
        if (eachPara as text)'s length > 0 then
            set end of recordsForSorting to my CheckText(eachPara as text)
        end if
    end repeat

    set sortedRecords to my bubblesort(recordsForSorting)

    set sortedText to ""
    repeat with eachRecord in sortedRecords
        set sortedText to sortedText & (eachRecord's ActualWord) & return
    end repeat

    return sortedText
end run

to CheckText(txt)
    if txt contains "<s>" then
        set sortRecord to {SortWord:((characters 4 thru -1 of txt) as text), ActualWord:txt}
    else if txt contains "the" then
        set sortRecord to {SortWord:((characters 5 thru -1 of txt) as text), ActualWord:txt}
    else
        set sortRecord to {SortWord:txt, ActualWord:txt}
    end if
    return sortRecord
end CheckText

-------------------------
on bubblesort(array)
    repeat with i from length of array to 2 by -1 --> go backwards
        repeat with j from 1 to i - 1 --> go forwards
            tell array
                if (item j)'s SortWord > (item (j + 1))'s SortWord then
                    set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
                end if
            end tell
        end repeat
    end repeat
    return array
end bubblesort

该脚本为包含实际文本的每个段落创建一个记录,并为“删除”或“删除”创建相同的文本。然后在目标项上使用气泡排序,按排序顺序返回实际文本时间的字符串。

我没有为星号做任何准备;我无法从您的帖子中确定这是原始问题的一部分,还是堆栈溢出格式化问题。

我没有测试它作为一个服务,但它工作在Automator与标准输入文本和随后输出到一个文本文件。

祝好运,

票数 0
EN

Stack Overflow用户

发布于 2015-08-12 21:51:29

通过使用@Craig给出的答案,我发现下面的脚本解决了我的问题,并有望对其他人有所帮助。

代码语言:javascript
复制
on run {input}

set rawParagraphs to every paragraph of (input as text)

set recordsForSorting to {}

repeat with eachPara in rawParagraphs
    if (eachPara as text)'s length > 0 then
        set end of recordsForSorting to my CheckText(eachPara as text)
    end if
end repeat

set sortedRecords to my bubblesort(recordsForSorting)

set sortedText to ""
repeat with eachRecord in sortedRecords
    set sortedText to sortedText & (eachRecord's ActualWord) & return
end repeat

return sortedText
end run

to CheckText(txt)

if txt starts with "    * <s>The " then
    set sortRecord to {SortWord:((characters 11 thru -1 of txt) as text), ActualWord:txt}

else if txt starts with "   * The " then
    set sortRecord to {SortWord:((characters 8 thru -1 of txt) as text), ActualWord:txt}

else if txt starts with "   * <s>" then
    set sortRecord to {SortWord:((characters 7 thru -1 of txt) as text), ActualWord:txt}

else if txt starts with "   * " then
    set sortRecord to {SortWord:((characters 4 thru -1 of txt) as text), ActualWord:txt}

else if txt starts with "* <s> The " then
    set sortRecord to {SortWord:((characters 11 thru -1 of txt) as text), ActualWord:txt}

else if txt starts with "* The " then
    set sortRecord to {SortWord:((characters 7 thru -1 of txt) as text), ActualWord:txt}

else if txt starts with "* <s>" then
    set sortRecord to {SortWord:((characters 6 thru -1 of txt) as text), ActualWord:txt}

else if txt starts with "* " then
    set sortRecord to {SortWord:((characters 3 thru -1 of txt) as text), ActualWord:txt}

else
    set sortRecord to {SortWord:txt, ActualWord:txt}
end if
return sortRecord
end CheckText

-------------------------
on bubblesort(array)
repeat with i from length of array to 2 by -1 --> go backwards
    repeat with j from 1 to i - 1 --> go forwards
        tell array
            if (item j)'s SortWord > (item (j + 1))'s SortWord then
                set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
            end if
        end tell
    end repeat
end repeat
return array
end bubblesort`

我怀疑有一些可怕的错误,我唯一的责任是潜伏在那里,但这似乎是可行的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31773900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档