LoginSignup
0
0

More than 5 years have passed since last update.

AppleScriptでMarkdown中のリンクファイルを収集する

Last updated at Posted at 2016-08-18

ああ、もうさわりたくない…
Finderで.mdを選択して実行すると、リンクファイルを同一階層にコピーし、リネームし、mdファイルへ反映させます。
使用は自己責任。というか自分でもハラハラ。

perlでパスのみ全て抜き出して、sort、uniq
パス名からファイルをコピーしてフォルダへ入れる
perlでさっきのパス名を検索(g)して置換

というわけで、フルパスで書いてある前提です。
一度実行すると元に戻りません。バックアップしてから使ってください。

(20160901追記)
パス置換がうまくいかネーゼ、なのでCotEditor経由にしてみた
あ、あとですね、ファイル名に()入ってるとあかんです。入れ子の正規表現書くのめんどくて放置

markdown_Correct4output.scpt
tell application "Finder"
    set mySel to selection as alias
    --  duplicate mySel
    set myMD to POSIX path of mySel
    set myMDName to name of mySel
    set myMDFolder to folder of mySel
    set myMDLinkFolder to (text 1 thru -4 of myMDName) & "_Links"
    set myMDLinkFolderPath to ((myMDFolder as text) & myMDLinkFolder as text)
    log myMDLinkFolderPath


    set myScript to "/usr/bin/perl -e 'open (DATAFILE,\"< " & myMD & "\");while (my $line =<DATAFILE>){chomp($line);while ($line =~m|(?<=\\]\\()(.+?)(?=\\))|g){print \"$&\\n\";}}' | /usr/bin/sort | /usr/bin/uniq"

    set myText to (do shell script myScript)
    log myText

    set delimitersTEMP to AppleScript's text item delimiters
    set AppleScript's text item delimiters to return
    set myPath to every text item of myText
    set AppleScript's text item delimiters to delimitersTEMP


    try
        make folder at myMDFolder with properties {name:myMDLinkFolder}
    end try
    set a to 1
    set myMDLinkRepName to {}
    tell application "CotEditor" to open mySel

    repeat with i in myPath

        set myDup to duplicate i as POSIX file as alias to folder myMDLinkFolderPath as alias with replacing
        set myMDLinkFileName to (text 1 thru -4 of (myMDName as text)) & "_" & a & "." & name extension of myDup
        set name of myDup to myMDLinkFileName
        set end of myMDLinkRepName to myMDLinkFileName
        --
        set myRepText to myMDLinkFolder & "/" & myMDLinkFileName
        tell front document of application "CotEditor" to replace for i to myRepText with all
        --  do shell script "/usr/bin/perl -pi -e 's|" & i & "|" & myMDLinkFolder & "/" & myMDLinkFileName & "|g' " & quoted form of myMD

        set a to a + 1
    end repeat
end tell
markdown_Correct4output.scpt
tell application "Finder"
    set mySel to selection as alias
    set myMD to POSIX path of mySel
    set myMDName to name of mySel
    set myMDFolder to folder of mySel
    set myMDLinkFolder to (text 1 thru -4 of myMDName) & "_Links"
    set myMDLinkFolderPath to ((myMDFolder as text) & myMDLinkFolder as text)
    log myMDLinkFolderPath


    set myScript to "/usr/bin/perl -e 'open (DATAFILE,\"< " & myMD & "\");while (my $line =<DATAFILE>){chomp($line);while ($line =~m|(?<=\\]\\()(.+?)(?=\\))|g){print \"$&\\n\";}}' | /usr/bin/sort | /usr/bin/uniq"

    set myText to (do shell script myScript)


    set delimitersTEMP to AppleScript's text item delimiters
    set AppleScript's text item delimiters to return
    set myPath to every text item of myText
    set AppleScript's text item delimiters to delimitersTEMP


    try
        make folder at myMDFolder with properties {name:myMDLinkFolder}
    end try
    set a to 1
    set myMDLinkRepName to {}
    repeat with i in myPath

        set myDup to duplicate i as POSIX file to myMDLinkFolderPath as alias with replacing
        set myMDLinkFileName to (text 1 thru -4 of (myMDName as text)) & "_" & a & (the time of (current date)) & "." & name extension of myDup
        set name of myDup to myMDLinkFileName
        set end of myMDLinkRepName to myMDLinkFileName
        do shell script "/usr/bin/perl -pi -e 's|" & i & "|" & myMDLinkFolder & "/" & myMDLinkFileName & "|g' " & quoted form of myMD
        log i
        set a to a + 1
    end repeat
end tell
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0