Mindjet MindManager上のデータをリストに展開してCSV書き出し

Mindjet MindManager上に展開したマインドマップを、選択中のトピック以下を対象に、いい感じにトピックのレベルを反映してCSV書き出しするAppleScriptです。

mj1.jpg

リスト上でトピックの配置を行い、Excelのワークシート上に……そのまま持って行くのは、不可能ではないにせよかったるい(r1c1形式とA1形式の変換サブルーチン(しかも巨大)を投入する必要がある)ため、以前にリストをCSV書き出しするサブルーチンを作ってあったので、CSV書き出ししておくことにしました。

mj2.jpg

書き出したCSVのファイルをExcelでオープンすれば、めんどくさい処理なしにExcelにマインドマップの内容を渡せます。

いい感じでマインドマップ上にアイデアを書き込んでいたところに「そのままだと分りづらいからExcelの表にして」などと言われ、一気にテンションが落ち……ないよう作ってみたものです。

風呂上がりにテレビを見ながら、いいかげんに作ったので……a reference toによる高速化を試みているものの、ろくに高速化されていなかったりしますが、目くじらを立てるほど遅くないため放っておきました。

スクリプト名:Mindjet MindManager上のデータをリストに展開してCSV書き出し
global gList, gList_r, levelList

set gList to {}
set gList_r to a reference to gList
set levelList to {}

tell application “Mindjet MindManager”
  tell document 1
    set aSel to selection
  end tell
  
set aaSel to first item of aSel
  
addMMSubtreeToGlobalVal(aaSel) of me
end tell

–行(row)の最大値、列の最大値(levelList)を取得する
set rowNum to length of gList_r
set colMax to maximumFromList(levelList) of me

–カラのリストを作成する
set blankList to makeBlankList(rowNum, colMax, “”) of me

–リストを作成する
set tmpCol to 1
set tmpRow to 1

set prevCol to 0
set prevRow to 0

set prevLevel to 0

repeat with i in gList_r
  set {aTitle, aLevel} to i
  
  
if aLevel < prevCol then
    set tmpRow to tmpRow + 1
  else if aLevel = prevCol then
    set tmpRow to tmpRow + 1
  else
    
  end if
  
  
–set item tmpRow of item aLevel of blankList to aTitle
  
set item aLevel of item tmpRow of blankList to aTitle
  
  
set prevCol to aLevel
  
set prevRow to tmpRow
  
end repeat

set aNewFile to choose file name
saveAsCSV(blankList, aNewFile) of me

–指定の大きさでカラのリストを作成する
on makeBlankList(rowMax, colMax, anItem)
  set allData to {}
  
  
repeat rowMax times
    set aRow to {}
    
repeat colMax times
      set the end of aRow to anItem
    end repeat
    
set the end of allData to aRow
  end repeat
  
  
return allData
end makeBlankList

–Mindjet MindManager上のトピックを渡すと、その子トピックの情報を収集して、サブトピックを再帰で取得
–結果はGlobal変数(gList)に追記する
–別のサブルーチンを書き換えてそのままの名前で使ってしまった
on addMMSubtreeToGlobalVal(mmTopic)
  tell application “Mindjet MindManager”
    repeat with childTopic in subtopics of mmTopic –指定トピック下の子トピックを取得するのに、この書き方しかMindjet MMが受け付けてくれない
      set topicTitle to (a reference to the title of childTopic)
      
set aLevel to level of childTopic
      
set aTitle to title of childTopic
      
set the end of gList_r to {aTitle, aLevel}
      
set the end of levelList to aLevel
      
addMMSubtreeToGlobalVal(childTopic) of me
    end repeat
  end tell
end addMMSubtreeToGlobalVal

–最大値を取得する
on maximumFromList(nList)
  script o
    property NL : nList
  end script
  
  
set max to item 1 of o’s NL
  
repeat with i from 2 to (count nList)
    set n to item i of o’s NL
    
if n > max then set max to n
  end repeat
  
return max
  
end maximumFromList

–CSV書き出し
–ただし、データ内にダブルクォートが入っていた場合に備えてのサニタイズ処理は行っていない
on saveAsCSV(aList, aPath)
  set crlfChar to (ASCII character 13) & (ASCII character 10)
  
set LF to (ASCII character 10)
  
set wholeText to “”
  
repeat with i in aList
    set aLineText to “”
    
set curDelim to AppleScript’s text item delimiters
    
set AppleScript’s text item delimiters to “\”,\”"
    
set aLineList to i as text
    
set AppleScript’s text item delimiters to curDelim
    
    
set aLineText to repChar(aLineList, return, “”) of me –データの途中に改行が入っていた場合には削除する
    
set aLineText to repChar(aLineText, LF, “”) of me –データの途中に改行が入っていた場合には削除する
    
    
set wholeText to wholeText & “\”" & aLineText & “\”" & crlfChar –行ターミネータはCR+LF
  end repeat
  
  
if (aPath as string) does not end with “.csv” then
    set bPath to aPath & “.csv” as Unicode text
  else
    set bPath to aPath as Unicode text
  end if
  
  
write_to_file(wholeText, bPath, false) of me
  
end saveAsCSV

–ファイルの追記ルーチン「write_to_file」
–追記データ、追記対象ファイル、boolean(trueで追記)
on write_to_file(this_data, target_file, append_data)
  try
    set the target_file to the target_file as text
    
set the open_target_file to open for access file target_file with write permission
    
if append_data is false then set eof of the open_target_file to 0
    
write this_data to the open_target_file starting at eof
    
close access the open_target_file
    
return true
  on error error_message
    try
      close access file target_file
    end try
    
return error_message
  end try
end write_to_file

–文字置換
on repChar(origText, targChar, repChar)
  set origText to origText as string
  
set targChar to targChar as string
  
set repChar to repChar as string
  
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to targChar
  
set tmpList to text items of origText
  
set AppleScript’s text item delimiters to repChar
  
set retText to tmpList as string
  
set AppleScript’s text item delimiters to curDelim
  
return retText
end repChar

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

Leave a Reply