• ベストアンサー

AppleScriptでデキストデータをExcel

AppleScriptでテキストファイルをExcelで開きたいのですが、 区切り文字をスペースと|の2つを指定したいのです。 スペースだけなら tell application "Microsoft Excel" open text file filename ¬ "Mac HD:Folder:File.txt" data type delimited with space end tell とできたのですが、|を組み合わせた場合がわかりません。 ご教授ください。

  • Mac
  • 回答数3
  • ありがとう数3

質問者が選んだベストアンサー

  • ベストアンサー
noname#250373
noname#250373
回答No.1

(1) ファイル数が少なければ、テキストエディタの類いを使って 「|」をスペースに置換したものを用意した方が早いと思います。 (2) Excel2011では2つの区切り文字でインポートできました。 Excelの「インポート」メニューで可能です。 操作の過程をスクリーンショットで撮りましたので、参考にしてみて下さい。 (3) てっきりExcelではできないと思い込んで、 以下のようなスクリプトを作ってしまいました(汗 --テキストファイルの「|」をスペースに置換したファイルを作成し、 --Excelでスペース区切りファイルとして読み込ませるAppleScript --OSX10.6.8 + Excel2011で動作確認 --ここで目的のファイルを選択(今回の場合、デスクトップの"File.txt") set aFile to (choose file with prompt "「|」区切りをスペースに変換したいファイルを選択して下さい") as alias --ファイルの内容を読み込み try open for access aFile set theBeforeText to read aFile as string end try close access aFile --ファイル名の取得 set aName to aFile as text if aName ends with ".txt" then set aName to characters 1 thru -5 of aName as text --as textにしないと文字バラバラ end if --デリミタによる区切り文字の置換。詳しくは --http://tonbi.jp/AppleScript/Tips/String/FindReplace.html --などを参考にしてみて下さい。 set tmp to AppleScript's text item delimiters try set AppleScript's text item delimiters to "|" set theTempText to every text item of theBeforeText set AppleScript's text item delimiters to space set theAfterText to theTempText as string end try set AppleScript's text item delimiters to tmp --区切り文字をスペースだけにしたファイルの作成(space_File.txt) set aOutPath to (aName & "_space.txt") as text try set bFile to open for access file aOutPath with write permission log class of aOutPath write theAfterText to file aOutPath on error tell current application activate display dialog "ファイル書き出し時にエラー発生!" & return & "書き出す予定のファイルと同じ名前のファイルが存在しませんか?" with icon 2 end tell end try close access bFile --あなたのスクリプトに受け渡し(Excelでオープン) delay 1 tell application "Microsoft Excel" open text file filename aOutPath data type delimited with space end tell

参考URL:
http://tonbi.jp/AppleScript/Tips/String/FindReplace.html
lee07465
質問者

お礼

ありがとうございます。 うまくできました。 中身は只今解読中… チョウ初心者なのでなかなかハードルが高いです。 でもがんばります。

その他の回答 (2)

noname#250373
noname#250373
回答No.3

無理に理解しようとしなくても大丈夫ですよ^-^ 個々の処理は基本的なものですので、 今は「こんな処理ができるんだ」と 心に留めてもらえるだけで十分です。 私も他の方のスクリプトを参考に 見よう見まねで組み立てている初心者ですから 偉そうなことは言えません(汗 お互い頑張りましょう!

lee07465
質問者

お礼

ありがとうございます。 みなさん、一歩一歩なんですね… 温かい言葉を胸にこれからも精進していきます!!

noname#250373
noname#250373
回答No.2

スクリーンショットの画像が荒すぎたので やや大きめのファイルをアップし直しました。

関連するQ&A

  • AppleScriptでpkgファイルの実行方法

    AppleScriptであるpkgファイルを実行させてインストール画面を表示させたいのですがどのように記述すればよいでしょうか。 tell application "Install" open "file.pkg" end tell ではできませんでした。 宜しくお願い致します。

    • ベストアンサー
    • Mac
  • applescript で pdfを扱いたいんです。

    applescriptで、pdfファイルをアクロバットリーダ6で開けるスクリプトを書いたのですがうまくいきません。どなたか教えて頂きませんか? プレビューで開けるならopen一発で簡単なのですが、アクロバットリーダで開けたいのです。 僕は以下のようなのを書いています。 もしや、アクロバットはpdfを開けるイベントハンドラは対応して無いのでしょうか? tell application "finder" open file "01.pdf" of desktop using application "Adobe Reader 6" end tell

    • ベストアンサー
    • Mac
  • indesignでのapplescript

    はじめまして、indesign CS3で使用するapplescriptに関しての質問です。 自動で出力をしてくれるスクリプトを作りたいと思い、やってみたのですがうまくいきません。 色々調べて作ってみたのがこれです。 tell application "Finder" set filesInFolder to files in (choose folder) repeat with theFile in filesInFolder if name extension of theFile is "indd" then -- 拡張子を指定 set priset to "B4" -- プリントプリセットを指定 tell application "Adobe InDesign CS3" activate open theFile tell document 1 print using priset without print dialog close saving no end tell end tell end if end repeat end tell ここまではなんとかできたのですが、現段階では1階層下までのinddドキュメントしか読取ってくれません。 1見開きごとにパッケージしてできた子フォルダを一つの親フォルダにまとめることが多いので、フォルダ▶フォルダ▶.inddになります。 これでは親フォルダを指定したときに2階層下の子フォルダに分かれて入っている複数の.inddを一気に出力することができません。 何か解決策はありませんでしょうか? また、現段階に関してのご指摘もございましたらよろしくお願いします。

  • applescriptでウインドウを閉じたい

    職場のローカルネットワーク内にあるmacのあるフォルダに接続するために以下のように書きました。 接続はうまくいきます。ただし同時にウインドウも開きます。 そのウインドウは不要であり、他のスタッフの目にもつくので閉じたいのですがうまくいきません。(とくにエラーも出ず、そのままウインドウが表示されたままになります) どうすればよろしいでしょうか。 tell application "Finder" activate open location "afp://***.***.***.***/Foleder/test.csv" close Finder window 1 end tell macOS10.8 Applescriptエディタ2.4.3 を使用しています

    • 締切済み
    • Mac
  • applescriptでのクリップボードへの格納方法

    applescript初心者です。 単純なことなのですが今、テキストファイル上の数字を検索しクリップボードに格納するscriptを作成挑戦中です。 tell application "Jedit X" tell front document find string "\\d+" with grep end tell end tell ※\は逆スラッシュ?です。 と、検索までは何とか辿り着きました。 ここから先、エディタ上では選択文字を単純にコピーした状態にするにはどうすれば良いのでしょうか? ご教授頂ければと思います。 よろしくお願いします。

    • ベストアンサー
    • Mac
  • AppleScriptがエラーになる

    osx10.8.4  AppleScriptエディタ2.5.1 にて以下の内容でFileMaker11へテキストデータを貼付けようとしましたところ ----------------------- set dbName to "test.fp7" tell application "FileMaker Pro Advanced" activate tell database dbName set contents of field "myText" to "ABC" end tell end tell ----------------------- error "FileMaker Pro Advanced でエラーが起きました:オブジェクトが見つかりません。 ----------------------- とのエラーが出ました。 ちなみにosx10.6.8 AppleScriptエディタ2.3ではテキストは読み込まれます。 osx10.8.4 の仕様が変わったのでしょうか 2・3日前にosのバージョンアップをしましたがそれが関係していたのでしょうか。 どなたかご存知の方がおられましたらご教授下さい。

    • ベストアンサー
    • Mac
  • Applescript バックグランドでメール送信

    以下のようなApplescriptがあります。 ファイルを開くと自動でメールを送信する物です。 件名: あいうえお 本文: かきくけこ 宛先: xxxxx@xxxxx.xx icalを使って定時送信をしているのですが、以下のスクリプトですと、起動するたびにメール作成の画面がフォーカスされてしまいます。常時バックグランドで処理をしたいのですがどうすればいいでしょうか。 ------------------------------------------------ tell application "Mail" set newMessage to make new outgoing message with properties {subject:"あいうえお", content:"かきくけこ", visible:true} tell newMessage to make new to recipient at end of to recipients with properties {address:"xxxxx@xxxxx.xx"} end tell tell application "System Events" tell application process "Mail" to set frontmost to true keystroke "T" using {command down, shift down} end tell tell application "System Events" tell application process "Mail" to set frontmost to true keystroke "D" using {command down, shift down} end tell ------

    • ベストアンサー
    • Mac
  • AppleScriptでiCalを動かしてみたが

    以前に下記のようなAppleScriptがネット上に有りましたが(1999年12月28日) ----------------------------------------------実行 set dSta to my date "2013年6月4日火曜日 0:00:00" set dEnd to my date "2013年6月4日火曜日 16:00:00" -- 検索するカレンダー名 set calName to "仕事" -- 結果を格納する変数 set copyText to "" tell application "Calendar" set theList to every event of calendar calName whose (start date ≥ dSta) and (end date ≤ dEnd) repeat with x in theList set aDate to (start date of x) set copyText to copyText & (aDate as text) & tab & summary of x & (ASCII character 10) end repeat end tell -- 結果をクリップボードにコピー set copyText2 to my Sortsc(copyText) set the clipboard to copyText2 on Sortsc(aData) do shell script "echo " & quoted form of aData & " | sort -d" end Sortsc ------------------------------------------------------------- 今回これを利用したい思い osx10.8 applescript エディタ2.5.1にて起動してみようとしましたが下記のような不思議な現象がでました。 set dSta to my date "2013年6月4日火曜日 0:00:00" set dEnd to my date "2013年6月4日火曜日 16:00:00" としていますが date "1999年12月28日火曜日 0:00:00" and end date ≤ date "1999年12月28日火曜日 16:00:00" となってしまった。 なぜそうなったのかが分かりません。 どなたかご存知のお方がおられましたらご教授いただきたいのですが。 -----------------------------------返された値 tell application "Calendar" get every event of calendar "仕事" whose start date ≥ date "1999年12月28日火曜日 0:00:00" and end date ≤ date "1999年12月28日火曜日 16:00:00" --> {} end tell tell current application do shell script "echo '' | sort -d" --> "" end tell tell application "AppleScript Editor" set the clipboard to "" end tell ------------------------------------------------------------- 因にosx10.6 AppleScript2.3にはきちんと返された値は内容が取得されています。

    • ベストアンサー
    • Mac
  • AppleScriptでアプリケーション再起動

    表題の件について教えてください。 VNCアプリケーションなんですが、動作が不安定で、 一旦遠隔で操作して接続を切ると、 当該アプリケーションを再起動しないと繋がらなくなるものがあります。 そこで、mailのメッセージルールで、 メール受信するとAppleScriptを実行するように設定して、 当該アプリケーションを再起動するようなapplescriptを作ってみましが 上手く動作してくれません。 AppleScriptを実行とすると、「接続が無効です」とのエラーメッセージが出て、 activateのところが水色のハイライトがつきます。 どのように修正したらよいか教えて下さい。 なお、実行するOSはlion10.7.4です。 tell application "アプリケーション名" quit activate end tell

    • ベストアンサー
    • Mac
  • applescriptで変数の代入について

    現在applescriptにて自動マウントのscript挑戦中です。 tell application "Finder" try mount volume "afp://●" delay 10 end try end tell のように作成したのですが、事前に変数aURLにアドレス文字列を設定して、その変数aURLを●に代入したいのですがget aURLとかset aURL等試しているのですがうまくいきません。 変数をどの様に呼び出すと良いのでしょうか。 よろしくお願いします。

    • ベストアンサー
    • Mac