将文件路径信息删除到•和/文本项分隔符

时间:2012-04-23 18:12:50

标签: applescript

下面的脚本基本上你选择一个带有PDF的文件夹,在所选文件夹上获取PDF文件的数量,将结果写入文本文件,在Excel中打开文本文件。脚本工作正常,但我得到了整个文件路径。

结果是:

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/:   65

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/:       RESENDS   0

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: 23

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/:     RESENDS   6

我想在子弹之前剥离一切•然后为每一个/一列剥离。像这样:

CUSTO_4    BODY    65

CUSTO_4    BODY          RESENDS   0

CUSTO_4    COVERS  23

CUSTO_4    COVERS        RESENDS   6

我试图掌握文本项分隔符的概念并使用offset命令,但我不知道如何将其实现到脚本中。

设置target_folder以选择带有提示的文件夹“选择仅包含PDF的目标文件夹来计算文件”,允许多个选项而不显示 将结果设置为“”

    repeat with i from 1 to (count target_folder)
        set thisFolder to (POSIX path of item i of target_folder)

    --Find & count all PDFs in folders selected that DON'T starts with letter R
    set fileCount to do shell script "find " & quoted form of thisFolder & " -type f  -name *.pdf -and -not -iname 'R[0-9-_]*.pdf' | wc -l"

    set results to (results & "" & thisFolder & ":" & fileCount & return)

    --Find & count all PDFs in folders selected that starts with letter R
    set fileCount to do shell script "find " & quoted form of thisFolder & " -type f -iname 'R[0-9-_]*.pdf' | wc -l"

    set results to (results & "" & thisFolder & ":" & tab & tab & "RESENDS" & fileCount & return)

    end repeat



--write results to a txt file

set theFilePath to (path to desktop folder as string) & "PDF File Count.txt"

set theFile to open for access file theFilePath with write permission

try

set eof of theFile to 0

write results to file theFilePath

close access theFile

on error

close access theFile

end try

--Will open the the PDF File Count.txt in Excel
tell application "Microsoft Excel"

activate

open text file filename "PDF File Count.txt"

end tell

2 个答案:

答案 0 :(得分:1)

AppleScript的文本项分隔符用于确定文本如何分解和/或重新组合。当您获取字符串的文本项时,字符串在每个分隔符处分开,结果是各个部分的列表。换句话说,如果你将一个文本项目列表强制转换为字符串,则会使用每个部分之间使用的分隔符重新组合这些部分。

对于您的示例,您可以使用以下内容(我添加了一些格式来获得结果):

set theList to {¬
    "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/:   65", ¬
    "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/:       RESENDS   0", ¬
    "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: 23", ¬
    "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/:     RESENDS   6"}

set finalResult to {} -- this will be the final result

set tempTID to AppleScript's text item delimiters -- stash the original delimiters
repeat with anItem in theList
    set AppleScript's text item delimiters to "•"
    set pieces to text items of anItem -- break apart at bullets
    log result

    set theFile to (rest of pieces) as text -- drop the first piece and reassemble
    set AppleScript's text item delimiters to "/"
    set pieces to text items of theFile -- now break apart at slashes
    log result

    set lastPiece to last item of pieces -- trim the last piece a bit
    set theCount to 0
    repeat while first character of lastPiece is in {space, ":"}
        set lastPiece to text 2 thru -1 of lastPiece -- trim it
        set theCount to theCount + 1 -- count up trimmed characters
    end repeat
    if theCount > 4 then set lastPiece to tab & tab & lastPiece -- add a little formatting...
    set last item of pieces to lastPiece -- put the trimmed piece back

    set text item delimiters to tab & tab
    set pieces to pieces as text -- put the pieces back together with tabs
    log result

    set end of finalResult to pieces -- store the reassembled text for later
end repeat
set AppleScript's text item delimiters to tempTID -- restore the original delimiters

choose from list finalResult with empty selection allowed -- show the results

答案 1 :(得分:0)

您不必总是使用文本项分隔符来操作文本:

set xxx to "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/:   65

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/:       RESENDS   0

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: 23

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/:     RESENDS   6"

set yyy to do shell script "echo " & quoted form of xxx & " | grep -o •.* | sed -e 's/•\\(.*\\):\\(.*\\)/\\1\\2/' -e 's/\\//        /'g"

和另一种方法:

set xxx to "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/:   65

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/BODY/:       RESENDS   0

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/: 23

/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•CUSTO_4/COVERS/:     RESENDS   6"

-- break apart and capture original delimiters
set {Astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/Volumes/PREPRESS/1_CATALOG/2_Press/PRINT_Catalog/2012/•"}
set yyy to text items 2 thru -1 of xxx
--put together
set AppleScript's text item delimiters to {""}
set yyy to yyy as string

-- break apart
set AppleScript's text item delimiters to ":"
set yyy to text items of yyy
--put together
set AppleScript's text item delimiters to {""}
set yyy to yyy as string

-- break apart
set AppleScript's text item delimiters to "/"
set yyy to text items of yyy
--put together
set AppleScript's text item delimiters to tab
set yyy to yyy as string

-- reset original delimiters
set AppleScript's text item delimiters to Astid
return yyy