Search code examples
macosapplescriptapplescript-objc

Applesricpt how to move files to /Library/Caches


I am working on a applesript code that allows users to customize the background of there login/change user screen. I am using basic applescript ui at the moment but hope to implement more advanced applescript-objC UI if this is a sucsess. At the moment i am having trouble moving the image "ch_pic" to /library/Caches can you please help me with this problem.

set ch_pic to (choose file)

--Creates variables for paths


--Creates application folder in user library for storing assets and code

tell application "Finder"
move file ch_pic to Library / Caches
end tell

--Moves user selected file to application folder

set the name of file ch_pic to "com.apple.desktop.admin.png"

If you ask i will put your name in the code of the finished product.


Solution

  • Pretty simple if you're just selecting, moving, and renaming a file. Just need to make sure that you're speaking the PATH style that Applescript understands.

    Here is an example of the code:

    -- Set ch_pic to chosen file (restricting to only recognized "image" types)
    set ch_pic to (choose file of type "public.image"))
    tell application "Finder"
    -- Set cache_folder to Caches destination (~/Library/Caches) as Applescript path (<hard drive name>:Users:<user name>:Library:Caches)
        set cache_folder to (((path to library folder from user domain as text) & "Caches"))
        -- Copy the file over
        copy file ch_pic to folder cache_folder
        -- Grab the full Applescript path of the newly placed file (two steps for clarity. 1: set the path. 2: explicitly inform Applescript that this is a file)
        set new_pic to ((cache_folder as text) & ":" & name of ch_pic)
        set pos_new_pic to (file new_pic)
        -- Perform the rename
        set the name of pos_new_pic to "com.apple.desktop.admin.png"
    end tell