LoginSignup
0
1

More than 5 years have passed since last update.

ドロップされたファイルをフォルダで包むDroplet(AppleScript)

Last updated at Posted at 2018-07-06

概要

  • フォルダに包まれたファイル
  • 包まれてない素のファイル

が混在したフォルダを整理しようかなと思ったけど
いちいち 新規フォルダ作成→リネーム→ファイル投げ込む とやるのが面倒になり
懐かしのdropletを作成してみた次第。

AppleScript、まとまった情報が残ってないので残しときます。

作り方

/Applications/Utilities/Script\ Editor.appを起動して、
適当に書いて、
アプリケーションとして保存、
とするとドロップレットのできあがり。

やること

  1. ドロップされたファイル(複数)について
  2. 拡張子を取り除いた名前で新規フォルダを作成し
  3. そこにファイルを移動

当然ながら、同名のファイル/フォルダは存在できないので、必要があれば一時的なファイル名で作って逃げる

ソース


on open objects
    tell application "Finder"
        repeat with obj in objects

            -- get name of folder to filename without extension, or same as filename
            set AppleScript's text item delimiters to "."
            set nameElements to every text item of (name of obj as string)
            set lastElementIndex to count nameElements
            if lastElementIndex > 1 then
                set folderName to (items 1 thru (lastElementIndex - 1) of nameElements) as string
            else
                set folderName to (name of obj as string)
            end if

            -- check same name exists
            set folderPath to ((folder of obj as string) & folderName)
            if not (exists folderPath) then

                -- create folder
                set newFol to make new folder at (folder of obj) with properties {name:folderName}

                -- move to it
                move obj to newFol

            else

                -- generate tmp foldername
                set tmpName to ""
                repeat 21 times
                    set tmpName to tmpName & some item of "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
                end repeat


                -- create folder
                set newFol to make new folder at (folder of obj) with properties {name:tmpName}

                -- move to it
                move obj to newFol

                -- rename folder
                set (name of newFol) to folderName
            end if


        end repeat
    end tell
end open

on run
    display alert "Dropped would be embedded with folder"
end run
0
1
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
1