如何创建 AppleScript 来循环浏览 BBEdit 文档并将“-”移动到从数字的末尾到开头?

发布于 2024-12-06 05:14:25 字数 857 浏览 1 评论 0原文

我有一个脏报告文件,其中包含格式不正确的负数。最终目标是将这些导入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£烟消云散 2024-12-13 05:14:25

我不确定您是否需要 AppleScript。 BBEdit 的查找/替换窗口可以解决这个问题吗?尝试以下操作:

Find: ([0-9\.]+)\-
Replace: \-\1

“Grep”和“Wrap around”应该是“替换”文本区域下方唯一选择的内容。然后单击“全部替换”。

如果您需要一次对一堆报告执行此操作,请使用相同的查找/替换模式进行多文件搜索。如果我没有正确理解你的问题,请告诉我,但我认为你可以在没有 AppleScript 的情况下完成此任务。

I'm not sure you need AppleScript. Will BBEdit's find/replace window work for this? Try the following:

Find: ([0-9\.]+)\-
Replace: \-\1

"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.

素年丶 2024-12-13 05:14:25

试试这个。我假设您可以将 bbedit (或其他地方)中的文本作为变量“originalText”获取到 applescript 中。然后你必须将“fixedText”放回它所属的位置。注意:在我的代码中,我假设“制表符”字符分隔 origText 的每一行中的单词/列,因为 Michael J. Barber 目前已对其进行格式化。如果它是另一个字符(例如空格字符),那么您必须将代码中的单词 tab 更改为 space

set originalText to "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"
set fixedText to fixNegativeSigns(originalText)

on fixNegativeSigns(theText)
    set listText to paragraphs of theText
    set fixedText to ""

    set {tids, text item delimiters} to {text item delimiters, tab}
    repeat with i from 1 to count of listText
        set listItems to {}
        set thisList to text items of (item i of listText)
        repeat with j from 1 to count of thisList
            set thisItem to item j of thisList
            if text -1 of thisItem is "-" then
                set thisItem to "-" & text 1 thru -2 of thisItem
            end if
            set end of listItems to thisItem
        end repeat
        set fixedText to fixedText & (listItems as text) & character id 10
    end repeat
    set text item delimiters to tids
    return text 1 thru -2 of fixedText
end fixNegativeSigns

注意:在测试上面的代码时,您应该将其复制/粘贴到 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.

set originalText to "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"
set fixedText to fixNegativeSigns(originalText)

on fixNegativeSigns(theText)
    set listText to paragraphs of theText
    set fixedText to ""

    set {tids, text item delimiters} to {text item delimiters, tab}
    repeat with i from 1 to count of listText
        set listItems to {}
        set thisList to text items of (item i of listText)
        repeat with j from 1 to count of thisList
            set thisItem to item j of thisList
            if text -1 of thisItem is "-" then
                set thisItem to "-" & text 1 thru -2 of thisItem
            end if
            set end of listItems to thisItem
        end repeat
        set fixedText to fixedText & (listItems as text) & character id 10
    end repeat
    set text item delimiters to tids
    return text 1 thru -2 of fixedText
end fixNegativeSigns

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文