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

时间:2011-09-22 23:05:56

标签: 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经验,所以我对脚本进行成像工作就像这个psudo脚本:

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,我完全失去了。

感谢您的帮助/

2 个答案:

答案 0 :(得分:1)

我不确定你需要AppleScript。 BBEdit的查找/替换窗口会为此工作吗?请尝试以下方法:

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

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

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

答案 1 :(得分:0)

试试这个。我假设您可以将bbedit(或任何地方)的文本作为变量“originalText”获取到AppleScript中。然后你必须把“fixedText”放回它所属的任何地方。注意:在我的代码中,我假设“tab”字符分隔origText每行中的单词/列,因为Michael J. Barber目前已将其格式化。如果是另一个角色(如空格角色),则必须将代码中的标签更改为空格

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 Editor中。您必须修复 originalText 中的制表符,因为它们无法复制/粘贴。因此,删除单词/列之间的空格并插入选项卡。然后代码将正常工作。