Applescript create folder with properties - applescript

This is my code:
on startButton_(sender)
set folderDestination to choose folder
log folderDestination
log nameOfFolder
tell application "Finder"
make new folder at folderDestination with properties {name:nameOfFolder}
end tell
end startButton_
The idea ist to make a folder at the folder Destination (folderDestination) with the name of the folder (nameOfFolder).
This is the error from the console
2021-03-13 09:28:02.844240+0100 Folder Creator[1571:39314] *** -[AppDelegate startButton:]: Finder got an error: Can’t make «class ocid» id «data optr0000000059FEE7F626C77C59» into type Unicode text. (error -1700)
But the log from the folder Destination and name did work:
2021-03-13 09:28:02.750713+0100 Media Folder Creator[1571:39314] file:///Users/name/Desktop/
and:
2021-03-13 09:28:02.753207+0100 Media Folder Creator[1571:39314] folderName123
The textfield in Xcode is connected to nameOfFolder.
Model Key Path: nameOfFolder
I do not know where the error is.
Thanks for your help.

The problem is nameOfFolder is an NSString object, you have to bridge it to AppleScript by coercing it to text:
make new folder at folderDestination with properties {name:nameOfFolder as text}
But since you are already in the Cocoa world it would be more efficient to create the folder with NSFileManager.
on startButton:sender
set folderDestination to POSIX path of (choose folder)
log folderDestination
log nameOfFolder
set fileManager to current application's NSFileManager's defaultManager()
set {success, fmError} to fileManager's createDirectoryAtPath:(folderDestination & nameOfFolder as text) withIntermediateDirectories:false attributes:(missing value) |error|:(reference)
if success is false then log fmError's localizedDescription()
end startButton:

Related

how to move files from most recently modified folder one level up using AppleScript (Big Sur)

I'm not a newbie to (or an expert on) scripting and programming, but after a few hours, I am stumped at how to copy a file (after having googled and tried various things for a few hours) using AppleScript in this situation.
I want to move a file from the most recently modified folder one level up. I'm trying to move the file:
/Users/tsg/my downloads folder/newest folder/pdfs/target1.pdf
to
/Users/tsg/my downloads folder/newest folder/target1.pdf
Where "newest folder" is not a constant name, but will change as I download newer folders.
When I run the code below,
tell application "Finder"
set downloadFolder to POSIX file "/Users/tsg/my downloads folder" as alias
set latestModifiedFolder to (downloadFolder & (name of last item of (sort every folder of downloadFolder by modification date)))
set targetFolder to latestModifiedFolder (* Is this necessary? *)
set pdfFolder to (targetFolder & ":pdfs:")
set pdfFile to (pdfFolder & ("target1.pdf"))
move pdfFile to targetFolder
end tell
I get the error
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type folder." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to folder
If I add the modifier folder: "move pdfFile to folder targetFolder" for the last line before "end tell", I get
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to integer"
If I add the modifier file "move file pdfFile to folder targetFolder", I get
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} to integer
If I remove the "move" line altogether, I get the result:
{alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"}
I would actually like to find the file based on a search term, declared as a property (Property searchWord : {"target"}), rather than hard coded, but I couldn't get that to work either.
Code:
set pdfFile to first file of pdfFolder whose name contains searchWord
Error:
"Can’t get file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}." number -1728 from file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}
There may be more than one pdf containing the word "target", and I'd actually like to move all files containing the search word up one level, and then rename them pdfs.one.pdf and pdfs.two.pdf.
Thanks in advance for any help.
The errors occur because you try to combine multiple incompatible classes.
When dealing with the Finder the Finder specifier syntax
file "foo" of folder "bar" of folder "baz" of startup disk
is a reasonable way to avoid those cannot-do-this errors
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFile to file "target1.pdf" of pdfFolder
move pdfFile to targetFolder
end tell
or to move all files containing "target" in the file name
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFiles to files of pdfFolder whose name contains "target"
move pdfFiles to targetFolder
end tell

Automatically move files of a certain type from folder A to B whenever they are added to folder A on macOS

Using Automator, I created a "Folder Action" to move files of certain types (i.e. "Kind IS Movie") from folder "FolderA" to folder "FolderB" as soon as they are added to folder "FolderA". It works fine except that it doesn't move files from subfolders (e.g. doesn't work if the movie files are added to "FolderA/SubA/"), and apparently there's no option on Automator to make it work on the subfolders.
Does anyone have any idea about how to either change that on Automator or how could I create a script for that? Thanks!
This following AppleScript folder action code should give you a general idea of how to accomplish what you are looking for. When a file or folder is added to a folder that this folder action is attached to, it will search all folders and sub folders for files (I used QuickTime movie as the kind of file... But you can change that to whatever you want) then those files will be moved to the folder that you will define in property moveToFolder
You can save this following code in Script Editor.app directly into your /Users/Your_User_Name/Library/Workflows/Applications/Folder Actions folder, as a .scpt file. Once the script is saved into that folder, it will be available to attach to any folder as a folder action using the Folder Actions Setup.
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Kind Of File To Act Upon
property fileKind : "QuickTime movie"
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose kind is fileKind)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to
OR Here Is A Version Which Defines The Name Extensions Of The Files To Act Upon
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Name Extensions Of Files To Act Upon
property videoFileExtensions : {"m4v", "mkv", "avi", "mp4", "mov", "wmv", "mpg"}
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose name extension is in videoFileExtensions)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to

Create folder structure on a networked drive

I'm trying to create a folder structure using applescript. This code works great locally, but when I try it on a networked drive it returns "Can't get <> "test" of application "Finder".
This is the code I have
tell application "Finder"
set campaignName to text returned of (display dialog "Please enter Campaign Name:" default answer "Campaign Name")
tell application "Finder" to set currentDirectory to target of Finder window 1
make new folder at currentDirectory with properties {name:campaignName}
make new folder at folder campaignName with properties {name:"a-creative"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"a-assets-from-client"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"b-art-director-files"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"c-for-production"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"d-developer-notes"}
make new folder at folder "b-art-director-files" of folder "a-creative" of folder campaignName with properties {name:"PSD"}
make new folder at folder "b-art-director-files" of folder "a-creative" of folder campaignName with properties {name:"storyboard"}
end tell
I'm hoping someone can help me figure out what I'm doing wrong here. I'm new to using applescript.
Thank you!
Try it this way:
tell application "Finder"
set campaignName to text returned of (display dialog "Please enter Campaign Name:" default answer "Campaign Name")
tell application "Finder" to set currentDirectory to target of Finder window 1
set projectFolderRoot to make new folder at currentDirectory with properties {name:campaignName}
set aCreativeRoot to make new folder at projectFolderRoot with properties {name:"a-creative"}
set bArtDirectorRoot to make new folder at aCreativeRoot with properties {name:"b-art-director-files"}
make new folder at aCreativeRoot with properties {name:"c-for-production"}
make new folder at aCreativeRoot with properties {name:"d-developer-notes"}
make new folder at bArtDirectorRoot with properties {name:"PSD"}
make new folder at bArtDirectorRoot with properties {name:"storyboard"}
end tell

Change icon of folder with AppleScript?

i've create an AppleScript very helpful to me and i wish if it is possible to automatically change the folder icon.
This script is very simple, it create one folder, then create one empty text file in the same folder.
Here is the script:
tell application "Finder"
set newfolder to make new folder with properties {name:My Folder}
make new file at newfolder with properties {name:"read_me.txt"}
end tell
Is it possible to automatically change the folder icon?
(I own my custom folder icon (.icns) in the same folder as the script, of course)
Heres a solution that uses a command line utility "seticon" found in this package: https://github.com/vasi/osxutils
It works on the assumption your script, icns file and new folder are all in the same directory.
tell application "Finder"
set parent_path to ((the container of the (path to me)) as text)
set target_folder_path to quoted form of POSIX path of (parent_path & "my folder")
set icon_path to quoted form of POSIX path of (parent_path & "icon.icns")
do shell script "/usr/local/bin/seticon -d " & icon_path & " " & target_folder_path
end tell
My Solution in 2021, using SF-Symbols
-- Help
-- folder_path : The folder whose icons will be changed (multiple selection in Finder possible)
-- icon_path : The Icon path
-- archiv_path : The Icon path for a folder named "Archiv"
-- Presets here as used by me
property folder_path : "/Users/ronny/Desktop/osxutils-master"
property icon_path : "/Volumes/Development/Developer/Xcode/LibPool/Icons/Folder.icns"
property archiv_path : "/Volumes/Development/Developer/Xcode/LibPool/Icons/Archiv.icns"
-- Frameworks and Additions
use framework "Foundation"
use framework "AppKit"
use scripting additions
-- Create list (array) with selected items
-- Be carefull, every icon will be changed
tell application "Finder"
set theListe to selection as list
end tell
repeat with i in theListe
set aName to name of i
log (aName)
set destPath to POSIX path of (i as text)
if aName is equal to "Archiv" then
set sourcePath to archiv_path
else
set sourcePath to icon_path
end if
set imageData to (current application's NSImage's alloc()'s initWithContentsOfFile:sourcePath)
(current application's NSWorkspace's sharedWorkspace()'s setIcon:imageData forFile:destPath options:2)
end repeat

Duplicate Files - AppleScript

I am trying to write an Apple Script that will duplicate two files from one folder to another. I tried to different methods but I keep getting an error.
Method 1:
tell application "Finder"
set home_path to home as text
set framework to alias (home_path & "Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.framework")
set bundle to alias (home_path & "Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.framework")
set destination to home_path & "Documents:WillowTree:BIA-Kelsey:BIA-Kelsey:ThirdParty"
duplicate file framework to folder destination with replacing
duplicate file bundle to folder destination with replacing
end tell
Method 2
tell application "Finder"
set framework to "Macintosh HD:Users:nayef:Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.framework"
set bundle to "Macintosh HD:Users:nayef:Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.bundle"
set destination to "Macintosh HD:Users:User:nayef:Documents:WillowTree:BIA-Kelsey:BIA-Kelsey:ThirdParty"
duplicate file framework to folder destination with replacing
duplicate file bundle to folder destination with replacing
end tell
The Error
error "Finder got an error: Can’t set folder \"Macintosh HD:Users:User:nayef:Documents:WillowTree:BIA-Kelsey:BIA-Kelsey:ThirdParty\" to file \"Macintosh HD:Users:nayef:Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.framework\"." number -10006 from folder "Macintosh HD:Users:User:nayef:Documents:WillowTree:BIA-Kelsey:BIA-Kelsey:ThirdParty"
What's wrong with my script?
This is because "ReaderKit.framework" is not a file, use folder, item or alias
duplicate folder framework to folder destination with replacing

Resources