AppleScriptの実行でデスクトップに異なる名前のフォルダが生成されない問題の解決方法

このQ&Aのポイント
  • AppleScriptでスクリプトを実行する際、デスクトップにフォルダ「t01」が生成されますが、2回目以降は「t02」や「t03」とはならずに「test」のままになってしまいます。
  • この問題は、OS 10.5.8で発生しているものです。間違った設定がされている可能性があります。
  • 解決方法として、まずFinderアプリケーションを使用して「test」フォルダをデスクトップに複製し、その後にフォルダ名を「t01」に変更する処理を加えることで、正しく異なる名前のフォルダを生成することができます。
回答を見る
  • ベストアンサー

AppleScript 添削希望

以下のスクリプトを1回実行するとデスクトップにフォルダ「t01」ができるのですが、 2回目以降「t02」とはならず、「test」のままで止まってしまいます。 OS 10.5.8 です。どこか間違ってるのでしょうか。 tell application "Finder" duplicate folder "test" of folder "_test" of desktop to desktop with replacing try set name of folder "test" of desktop to "t01" on error try set name of folder "test" of desktop to "t02" on error try set name of folder "test" of desktop to "t03" end try end try end try end tell

  • toast5
  • お礼率90% (110/121)
  • Mac
  • 回答数1
  • ありがとう数1

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

  • ベストアンサー
  • capy2009
  • ベストアンサー率72% (337/465)
回答No.1

名前を変える時に同名のファイル/フォルダが存在しても、エラーを返さずにそのまま終了するようですね。 エラーが出るかどうかではなく、同名のフォルダが存在するかどうかを直接判定する必要があるでしょう。 tell application "Finder" duplicate folder "test" of folder "_test" of desktop to desktop with replacing if not (exists folder "t01" of desktop) then set name of folder "test" of desktop to "t01" else if not (exists folder "t02" of desktop) then set name of folder "test" of desktop to "t02" else if not (exists folder "t03" of desktop) then set name of folder "test" of desktop to "t03" end if end tell で一応は動くはずです

toast5
質問者

お礼

if を使った条件分岐のサンプルをネット上で見かけるたびに、 ああ、こういうのが使えるようになれば、できることの幅が大きく広がるんだろう、 などと思っていたのですが、なるほど、こういう具合に使えるのですね。 目から鱗です。ありがとうございました。

関連するQ&A

  • AppleScript、ファイルの移動について

    初心者です、以下の文で、デスクトップにある test.txt というファイルをアプリケーションフォルダーにある test というフォルダーに移動させたいのですが、エラーが出ます。 ------------------------------ tell application "Finder" set tgtFile to file "test.txt" of desktop move tgFile to folder "test" of applications folder with replacing end tell ------------------------------ エラー文 error "folder \"test\" of applications folder を取り出すことはできません。" number -1728 from «class cfol» "test" of At Ease applications folder ------------------------------ しっかりアプリケーションフォルダーにtestというファイルはあります。 改善法を教えてください<(_ _)>

    • ベストアンサー
    • Mac
  • AppleScriptで教えて欲しいです。

    現在、clipboardと同じ名前のフォルダー1つ(例:ABC123)のみ処理する。 →これを、clipboardの名前を含むフォルダー1つ(例:ABC123.4)のみ処理する。 に変更したいです。 ちなみにclipboardの名前を含むフォルダーは、1つしか存在しません。 申し訳ありませんが、実際の業務で使用している下記のスクリプト(無駄な部分も多少あるかと思いますが)を出来る限り変更を加えず、可能でしょうか? 無理であれば、大幅変更も可能ですので、ご教授の程、宜しくお願い致します。 global Num global fp set Num to the clipboard tell application "Finder" my shikyuseihan2data() --支給製版2課データ try duplicate fp to folder "data" of disk "HD" move fp to folder "jus:5)製版行き: 2課使用済" end try end tell --アクセス権 tell application "Terminal" activate do script with command "chmod -R 775 /Volumes/HD/data" quit end tell --支給製版2課のデータ on shikyuseihan2data() tell application "Finder" set x to name of every disk if "jus" is in x then else mount volume "afp://10.1.30.10/jus/" end if try set fp to "jus:5)製版行き:" & Num & ":" as text set fp to fp as alias end try end tell end shikyuseihan2data

    • ベストアンサー
    • 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
  • 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でのファイルコピー

    macmini server (SnowLeopard:OSX10.6) 上記機種を使用。 macの知識がなく、最近applescriptという機能を知りました。 使い方が全くわからない状態でしたが、 今回、NAS(マウント済み)にあるフォルダAの中のファイルBを macのServer HDの中にあるフォルダCへコピーする作業を 自動化させたいと思い、ファイルコピーの方法について調べていたところ、 とある質問サイトで下記のコードを見つけ、入力項目を埋めて実行しました。 が、「必要なボリュームがマウントされていません」とエラーがでました。 既にマウントしているのは確認しています。 恐らくボリューム名かファイルパスの入れ方が違うのかなと思い、 いろいろと試してみましたが、出来ませんでした。 ここでいうボリューム名とファイルパスとフォルダパスは どのような入れ方をすればよいのでしょうか? もしくは、他の原因であれば、その解決策を教えて頂けると 助かります。 すみませんが宜しくお願い致します。 ------ ペーストするコード (以下最後まで) -------------------- ----【入力項目】------------ set FromVolumName to "ボリューム名を入れる" ---(↑説明)コピー元となるファイルのあるボリューム名 set ToVolumName to "ボリューム名を入れる" ---(↑説明)コピー先となるフォルダのあるボリューム名 set FromCopyFile to "ファイルパスを入れる" ---(↑説明)コピー元となるファイルのパス set ToCopyFolder to "フォルダパスを入れる" ---(↑説明)コピー先となるフォルダのパス set CP_opt to "-f" ---(↑説明)cpコマンドのオプション ------------------------- tell application "Finder" set {MyMsg, MyIcn} to {"完了!", 1} if (exists disk FromVolumName) and (exists disk ToVolumName) then try do shell script "cp " & CP_opt & " " & quoted form of POSIX path of FromCopyFile & " " & quoted form of POSIX path of ToCopyFolder on error set {MyMsg, MyIcn} to {"なんらかのエラーです!", 0} end try else set {MyMsg, MyIcn} to {"必要なボリュームがマウントされていません!", 0} end if activate display dialog MyMsg buttons {"終了"} default button 1 with icon MyIcn end tell

    • ベストアンサー
    • Mac
  • AppleScriptでpathを取得

    クリックしたファイルの絶対パスをクリップボードに保存する AppleScript(以下に記載)があるのですが、これでは クリップボードの中身が '/Users/ABC/Desktop/tmp1.jpg' '/Users/ABC/Desktop/tmp2.jpg' となります。 クオーテーション「 ' 」なしで、クリップボードに保存したいのですが どのようにすれば良いのでしょうか。 rest of characters of などを使ってどうにか組もうとしたのですが 初心者にはできませんでした。 どなたか、よろしくお願いします。 tell application "Finder" set theItems to "__OBJ_PATH__" set theResult to "" set old_delims to AppleScript's text item delimiters set AppleScript's text item delimiters to ":" set max to count of (every text item of theItems) repeat with num from 1 to max set theResult to theResult & quoted form of (text item num of theItems) if max > 1 and num < max then set theResult to "theResult" & " " end if end repeat set AppleScript's text item delimiters to old_delims set the clipboard to theResult end tell

  • 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でduplicateした後に

    applescriptでのファイル操作に関して質問させて頂きます。 宜しくお願いいたします。 現在、macOS10.8.5で、applescriptのコードを書いております。 tell application "Finder" set mynewfile to (duplicate myfile to toDir) のように、ファイルをコピーした後に、コピー先のファイルを別のアプリケーション(今回はphotoshop CS6)で開いて、処理を継続したいと考えています。 duplicateの戻り値は、class:document fileで、mynewfileから /Users/macbook/Desktop/toDir/myfile.jpg のようなUNIX形式のパステキストを作って渡せば良いと考えたのですが、どうしても取得できません。 ダメもとで、 tell application "Adobe photoshop CS6" open mynewfile などとしても、エラーで動きませんが、 単純に、 open mynewfile とすると、拡張子に対応したデフォルトのアプリケーションでファイルが開きます。 そこで以下2つ質問をさせて頂きます。 質問1) コピーしたファイルを指定したアプリケーションで開くためには、どのような処理を記述すれば良いのでしょうか? 質問2) duplicateの戻り値から、UNIX形式のパステキストを得るためには、どのような処理が適切でしょうか? applescriptに詳しい方、どうかご教示をお願いいたします。

    • ベストアンサー
    • 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で

    kのディレクトリ内にあるインデックス番号がjのファイルの名前をiにだいにゅうしたくて set i to name of item index j of k ト書きましたがエラーになりました。なぜでしょう。助けてください。

    • ベストアンサー
    • Mac