运行 Javascript 通过 Apple Automator 排序
Running a Javascript Sort Through Apple Automator
我正在尝试在 Apple Automator 中使用以下代码
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>
and/or "The "。* 需要保留在列表中以保持列表)。
将它从 iPhone 转移到 Automator 是事情分崩离析的地方,因为 Automator 找不到变量 getSelectedLineRange。我猜这是 Automator 处理文本输入的方式与脚本想要获取和处理它的方式之间的冲突,但我在如何解决它方面陷入僵局。
举个例子(如果我的整个方法都是错误的)我想要这个列表,在我可以输入的任何文本字段中
* Armadillo
* The aardvark
* <s>Rhino</s>
* <s>The Zebra</s>
* The Giraffe
* Hedgehog
选中后,通过脚本,运行作为服务,出来是这样的
* The aardvark
* Armadillo
* The Giraffe
* Hedgehog
* <s>Rhino</s>
* <s>The Zebra</s>
我当然不喜欢 javascript 解决方案,但这是我的起点。
我对 JavaScript 不够熟悉,无法提供帮助,但是这个 AppleScript 应该可以在 运行 AppleScript Action:
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
脚本为每个段落创建一个记录,其中包含实际文本,以及删除了 "the" 或“”的相同文本。然后它对目标术语使用冒泡排序,按排序顺序返回实际文本时间的字符串。
星号我没有做任何规定;我从你的 post 那里不确定这是原始问题的一部分,还是堆栈溢出格式化问题。
我没有将其作为服务进行测试,但它在 Automator 中使用标准输入文本并随后输出到文本文件。
祝你好运,
根据@Craig 给出的答案,我发现以下脚本可以解决我的问题,并希望对其他人有所帮助。
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`
我怀疑有一些可怕的错误是我潜伏在那里的唯一原因,但它似乎有效。
运行 JavaScript" Automator 中的操作需要 run(input, parameters)
函数。
input 是一个包含一项(文本)的数组。
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');
}
我正在尝试在 Apple Automator 中使用以下代码
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>
and/or "The "。* 需要保留在列表中以保持列表)。
将它从 iPhone 转移到 Automator 是事情分崩离析的地方,因为 Automator 找不到变量 getSelectedLineRange。我猜这是 Automator 处理文本输入的方式与脚本想要获取和处理它的方式之间的冲突,但我在如何解决它方面陷入僵局。
举个例子(如果我的整个方法都是错误的)我想要这个列表,在我可以输入的任何文本字段中
* Armadillo
* The aardvark
* <s>Rhino</s>
* <s>The Zebra</s>
* The Giraffe
* Hedgehog
选中后,通过脚本,运行作为服务,出来是这样的
* The aardvark
* Armadillo
* The Giraffe
* Hedgehog
* <s>Rhino</s>
* <s>The Zebra</s>
我当然不喜欢 javascript 解决方案,但这是我的起点。
我对 JavaScript 不够熟悉,无法提供帮助,但是这个 AppleScript 应该可以在 运行 AppleScript Action:
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
脚本为每个段落创建一个记录,其中包含实际文本,以及删除了 "the" 或“”的相同文本。然后它对目标术语使用冒泡排序,按排序顺序返回实际文本时间的字符串。
星号我没有做任何规定;我从你的 post 那里不确定这是原始问题的一部分,还是堆栈溢出格式化问题。
我没有将其作为服务进行测试,但它在 Automator 中使用标准输入文本并随后输出到文本文件。
祝你好运,
根据@Craig 给出的答案,我发现以下脚本可以解决我的问题,并希望对其他人有所帮助。
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`
我怀疑有一些可怕的错误是我潜伏在那里的唯一原因,但它似乎有效。
运行 JavaScript" Automator 中的操作需要 run(input, parameters)
函数。
input 是一个包含一项(文本)的数组。
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');
}