如何创建 AppleScript 来循环浏览 BBEdit 文档并将“-”移动到从数字的末尾到开头?
我有一个脏报告文件,其中包含格式不正确的负数。最终目标是将这些导入 Excel 进行分析。我在导入 Excel 之前使用 BBEdit 清理报告。我想创建一个苹果脚本来循环遍历报告并将“-”从数字后面移到前面。
脏报告示例行:
B-EXCAL 02 3684 2.0000- 49.02- 108.00- 58.98- 54.6-
B-MISMH 09-3300 33.0000 722.91 1353.00 630.09 46.6
所需的输出:
B-EXCAL 02 3684 -2.0000 -49.02 -108.00 -58.98 -54.6
B-MISMH 09-3300 33.0000 722.91 1353.00 630.09 46.6
我有 JavaScript 和 VBScript 经验,因此我想象脚本的工作方式类似于此伪脚本:
Get contents of current window from BBEdit
for each word in contents
if char(len(word)) = "-"
newWord = "-" + rightTrim(word, 1)
replace(word, newWord)
end if
end for
end
这是我第一次使用 AppleScript,我完全不知所措。
感谢您的帮助/
I have a dirty report file that contains improperly formatted negative numbers. The ultimate goal is to import these to Excel for analysis. I am using BBEdit to clean the report before importing into Excel. I would like to create an apple script to loop through the report and move the "-" from the back of the number to the front.
Dirty Report Example Rows:
B-EXCAL 02 3684 2.0000- 49.02- 108.00- 58.98- 54.6-
B-MISMH 09-3300 33.0000 722.91 1353.00 630.09 46.6
Desired output:
B-EXCAL 02 3684 -2.0000 -49.02 -108.00 -58.98 -54.6
B-MISMH 09-3300 33.0000 722.91 1353.00 630.09 46.6
I have JavaScript and VBScript experience so I imaging the script working something like this psudo script:
Get contents of current window from BBEdit
for each word in contents
if char(len(word)) = "-"
newWord = "-" + rightTrim(word, 1)
replace(word, newWord)
end if
end for
end
This is my first experience with AppleScript and I am at a complete loss.
Thanks for the help/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您是否需要 AppleScript。 BBEdit 的查找/替换窗口可以解决这个问题吗?尝试以下操作:
“Grep”和“Wrap around”应该是“替换”文本区域下方唯一选择的内容。然后单击“全部替换”。
如果您需要一次对一堆报告执行此操作,请使用相同的查找/替换模式进行多文件搜索。如果我没有正确理解你的问题,请告诉我,但我认为你可以在没有 AppleScript 的情况下完成此任务。
I'm not sure you need AppleScript. Will BBEdit's find/replace window work for this? Try the following:
"Grep" and "Wrap around" should be the only things selected below the "Replace" text area. Then click "Replace All."
If you need to do it to a bunch of reports at once, use the same find/replace patterns with multi file search. If I'm not understanding your question correctly, let me know, but I think you can get this done without AppleScript.
试试这个。我假设您可以将 bbedit (或其他地方)中的文本作为变量“originalText”获取到 applescript 中。然后你必须将“fixedText”放回它所属的位置。注意:在我的代码中,我假设“制表符”字符分隔 origText 的每一行中的单词/列,因为 Michael J. Barber 目前已对其进行格式化。如果它是另一个字符(例如空格字符),那么您必须将代码中的单词 tab 更改为 space。
注意:在测试上面的代码时,您应该将其复制/粘贴到 Applescript 编辑器中。您必须修复originalText中的制表符,因为它们在复制/粘贴后无法保留。因此,删除单词/列之间的空格并插入制表符。然后代码就可以正常工作了。
Try this. I'm assuming you can get the text from bbedit (or wherever) into applescript as the variable "originalText". Then you'll have to put the "fixedText" back wherever it belongs. NOTE: in my code I assume the "tab" character separates the words/columns in each line of the origText as Michael J. Barber has it currently formatted. If it is another character (like a space character) then you will have to change the word tab in the code to space.
NOTE: when testing my code above you should copy/paste it into Applescript Editor. You will have to fix the tab characters in originalText because they do not survive the copy/paste. So remove the spaces between the words/columns and insert a tab. Then the code will work correctly.