Skip to content

Instantly share code, notes, and snippets.

@AJABON
Last active October 22, 2025 05:10
Show Gist options
  • Select an option

  • Save AJABON/6b72277f2accf6b4313f356895e12b2f to your computer and use it in GitHub Desktop.

Select an option

Save AJABON/6b72277f2accf6b4313f356895e12b2f to your computer and use it in GitHub Desktop.
macOSのJXA: Finder項目をドロップすると同じ名前のフォルダを作成した中に移動するやつ
function openDocuments(docs) {
var app = Application.currentApplication();
app.includeStandardAdditions = true;
for (var i = 0; i < docs.length; i++) {
var filePath = docs[i].toString();
// ファイル名と拡張子を除去
var fileName = filePath.split("/").pop();
var baseName = fileName.replace(/\.[^\.]+$/, "");
var dirPath = filePath.replace(/\/[^\/]+$/, "");
var newFolderPath = dirPath + "/" + baseName;
// mkdir(存在しなければ作る)
try {
app.doShellScript('mkdir -p "' + newFolderPath + '"');
} catch (e) {}
// mv(同名があっても上書き)
try {
app.doShellScript('mv -f "' + filePath + '" "' + newFolderPath + '/"');
} catch (e) {}
// (任意)ログ出力
console.log("Moved: " + filePath + " → " + newFolderPath);
}
}
@AJABON
Copy link
Author

AJABON commented Oct 10, 2025

JXAのコードです。
スクリプトエディタ.appで新規ドキュメントを開き、ペーストして、
種類が「AppleScript」になっていたら「JavaScript」を選択して、
アプリケーションとして保存してください。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment