Text AppleScript和.txt文件

Text AppleScript和.txt文件,text,applescript,Text,Applescript,我需要有一个AppleScript,它将编辑给定文本文件通用结构的内容,删除第5个字符到第8个字符,保留第9-20个字符,并删除第21-32个字符。例如: 假设这是我的文本文件: Qt&:$yp$shshshahahah$JSJAJSSJH 单线 我需要删除从第一个$到下一个$,然后删除包括最后一个$之后的所有内容。在本例中,最终结果如下: Qt&:shshshahaha 谢谢, Isaac D'Keefe您可以在终端中运行如下命令: sed -Ei '' 's/(.{4}).{4}(.{1

我需要有一个AppleScript,它将编辑给定文本文件通用结构的内容,删除第5个字符到第8个字符,保留第9-20个字符,并删除第21-32个字符。例如:

假设这是我的文本文件:

Qt&:$yp$shshshahahah$JSJAJSSJH

单线

我需要删除从第一个$到下一个$,然后删除包括最后一个$之后的所有内容。在本例中,最终结果如下:

Qt&:shshshahaha

谢谢,
Isaac D'Keefe

您可以在终端中运行如下命令:

sed -Ei '' 's/(.{4}).{4}(.{12}).*/\1\2/' ~/Documents/file
或者,如果需要使用AppleScript:

set p to "/Users/username/Documents/file"
set input to read p as «class utf8»
set input to text 1 thru 4 of input & text 9 thru 20 of input
set fd to open for access p with write permission
set eof fd to 0
write input to fd as «class utf8»
close access fd
我需要从第一个$开始删除到下一个$,并且 然后删除包含最后一个$后的所有内容。在这个例子中, 最终结果将是:

根据这些条件,您可以使用分隔符/分隔符将字符串拆分为块。那么据我所知,你只想保留奇数项,并通过索引删除偶数项。因此,下面的脚本将按照您所描述的那样工作,而不是处理字符的索引

set theString to " Qt&:$yp$shshshahahah$jsjsjajssjh "

set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$"}
set textItems to every text item of theString
set AppleScript's text item delimiters to oldTID

set filteredTextItems to {}
repeat with x from 1 to count textItems by 2
    set end of filteredTextItems to item x of textItems
end repeat

return filteredTextItems as string

欢迎来到堆栈溢出。询问代码的问题必须证明对正在解决的问题的最低理解。包括尝试过的解决方案、它们不起作用的原因以及预期结果。