Open user's library folder using Applescript - applescript

I would like to open a folder that defaults to an application's settings folder:
/Users/XXX/Library/Application Support/Tunnelblick/Configurations
I don't want to 'hard code' the 'XXX'.
The /library folder:
set theDestination to (choose folder with prompt "Open destination folder" default location (path to library folder))
The home folder:
set theDestination to (choose folder with prompt "Open destination folder" default location (path to home folder))
I'm not sure how to get the ~/Library folder and one of its subfolders.

The paths to the librarys are:
POSIX path of (path to library folder from user domain)
= ~/Library/
POSIX path of (path to library folder from local domain)
= /Library/
POSIX path of (path to library folder from system domain)
= /System/Library/

The system also knows about the application support folder:
set aFolderPath to ((path to application support folder from user domain) as text) & "Tunnelblick:Configurations:"
try
set destinationPath to aFolderPath as alias
on error
beep
log aFolderPath & " missing"
return
end try
set theDestination to (choose folder with prompt "Open destination folder" default location destinationPath)
See StandardAdditions, path to
path to v : Return the full path to the specified folder
path to application support/applications folder/desktop/desktop pictures folder/documents folder/downloads folder/favorites folder/Folder Action scripts/fonts/help/home folder/internet plugins/keychain folder/library folder/modem scripts/movies folder/music folder/pictures folder/preferences/printer descriptions/public folder/scripting additions folder/scripts folder/services folder/shared documents/shared libraries/sites folder/startup disk/startup items/system folder/system preferences/temporary items/trash/users folder/utilities folder/workflows folder/voices/apple menu/control panels/control strip modules/extensions/launcher items folder/printer drivers/printmonitor/shutdown folder/speakable items/stationery : the folder to return
[from system domain/local domain/network domain/user domain/Classic domain] : where to look for the indicated folder
[as type class] : the type to return: alias or string (default is alias)
[folder creation boolean] : Create the folder if it doesn’t exist? (default is true)
→ alias : the path to the specified folder

Solved:
set theDefault to (path to home folder as text) & "Library:Application Support:Tunnelblick:Configurations" as alias
set theDestination to (choose folder with prompt "Open destination folder" default location theDefault)

Related

macOS 13.1 (22C65) - AppleScript to copy local Dropbox folder files

Can you review this code and tell me what I'm doing wrong. I keep getting
(Expected “,” but found identifier)
set baseFolder to (path to home folder as text) & "Dropbox/My Documents/GreenStripeMedia"
set projectName to "NewProject"
if not (exists folder baseFolder) then
display dialog "The base folder doesn't exist." buttons {"OK"} default button 1 with icon caution
return
end if
if not (folder baseFolder is writable) then
display dialog "You don't have write permissions to the base folder." buttons {"OK"} default button 1 with icon caution
return
end if
if (exists folder (baseFolder & "/" & projectName)) then
display dialog "A folder with that name already exists in the base folder." buttons {"OK"} default button 1 with icon caution
return
end if
set newProjectFolder to baseFolder & "/" & projectName
make new folder at baseFolder with properties {name:projectName}
set masterFolder to baseFolder & "/Master"
tell application "Finder"
set masterFiles to files of masterFolder
repeat with aFile in masterFiles
duplicate aFile to newProjectFolder
end repeat
end tell
Here's what it should all do
Creates a folder in the specified directory "$HOME/Dropbox/My Documents/GreenStripeMedia" with the name "Project Name".
Copies a file named "Content Template.docx" from "$HOME/Dropbox/My Documents/GreenStripeMedia/Master" and renames it to "Project Name Content.docx" in the new directory.
Copies a file named "Logins Template.docx" from "$HOME/Dropbox/My Documents/GreenStripeMedia/Master" and renames it to "Project Name Logins.docx" in the new directory.
Creates an empty directory in "$HOME/Dropbox/My Documents/GreenStripeMedia/Project Name" named "100 Logos".
Also check if the base folder is writable, and displays an error message if it's not.

Applescript create folder with properties

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:

Applescript - duplicate file to folder

(noob / novice) here I have done many searches online but cannot get this to work
I am trying to get a file duplicated from a pre-determined location into a newly created folder from within the script.
The script basically asks for a few bits of info and creates a folder structure using the info and a predefined set of folder within. from there I would want it to copy some template files (.psd .fcpbundle etc) from a template folder on the HDD.
my code is as follows now it all works up until it tries to copy the file
tell application "Finder"
set JobName to text returned of (display dialog "Please enter the wedding date" default answer "YYYY/MM/DD") --Asks for date of wedding
set DT to text returned of (display dialog "Please enter the couples name" default answer "Bride & Groom + Surname") --Asks for couples name
set Wedding to text returned of (display dialog "Please enter the type of day" default answer "Wedding") --Asks for type of day, Wedding, Party, etc
--Creates directory with info from above
set loc to choose folder "Please choose where you would like to save the files" --Loc = Location of where directory will be placed
set newfoldername to JobName & " - " & DT & " - " & Wedding --Adds Date, Couples name and event type to make directory name
--Creates parent directory
set newfo to make new folder at loc with properties {name:newfoldername} --Directory name defined
set audio to make new folder at newfo with properties {name:"Audio"} --creates Audio Directory
set doc to make new folder at newfo with properties {name:"Documents"} --creates Documents Directory
set exports to make new folder at newfo with properties {name:"Exports"} --creates Exports Directory
set fpcx to make new folder at newfo with properties {name:"FCPX Library"} --creates FCPX Directory
set filmposter to make new folder at newfo with properties {name:"Film Poster"} --creates FilmPoster Directory
set finaldel to make new folder at newfo with properties {name:"Final Delivery"} --creates Final Delivery Directory
set images to make new folder at newfo with properties {name:"Images"} --creates Images Directory
set rawcards to make new folder at newfo with properties {name:"RAW Cards"} --creates RAW Cards Directory
set xml to make new folder at newfo with properties {name:"XML"} --creates XML Directory
--Creates sub-folders
set submusic to make new folder at audio with properties {name:"Music"} --creates Music in Audio Directory
set subrawaudio to make new folder at audio with properties {name:"RAW Audio Cards"} --creates RAW Audio Cards in Audio Directory
set subdvd to make new folder at finaldel with properties {name:"DVD"} --creates DVD in Final Delivery Directory
set subusb to make new folder at finaldel with properties {name:"USB"} --creates USB in Final Delivery Directory
set subweb to make new folder at finaldel with properties {name:"WEB"} --creates WEB in Final Delivery Directory
set subgraphics to make new folder at images with properties {name:"Graphics"} --creates Graphics in Images Directory
set substills to make new folder at images with properties {name:"Stills"} --creates Stills in Graphics Directory
set subcam_1 to make new folder at rawcards with properties {name:"Cam-1"} --creates Cam-1 in RAW Cards Directory
set subcam_2 to make new folder at rawcards with properties {name:"Cam-2"} --creates Cam-2 in RAW Cards Directory
set subcam_3 to make new folder at rawcards with properties {name:"Cam-3"} --creates Cam-3 in RAW Cards Directory
--Moves files from template directory to working directory
set sourceFile to POSIX file "/Users/ricoh-admin/Movies/WeddingTemplate Creator/2005_0001.rtf"
set destFolder to POSIX file "loc & finaldel & subdvd"
tell application "Finder"
duplicate POSIX file sourceFile to destFolder
end tell
end tell
Any help would be appreciated and to know where I went wrong etc
You are mixing up POSIX paths and HFS paths.
This version creates the entire folder structure in one line via the shell (this requires a slash separated POSIX path)
The Finder duplicates the RTF file which requires colon separated HFS paths.
tell application "Finder" to set frontmost to true
set JobName to text returned of (display dialog "Please enter the wedding date" default answer "YYYY/MM/DD") --Asks for date of wedding
set DT to text returned of (display dialog "Please enter the couples name" default answer "Bride & Groom + Surname") --Asks for couples name
set Wedding to text returned of (display dialog "Please enter the type of day" default answer "Wedding") --Asks for type of day, Wedding, Party, etc
--Creates directory with info from above
set loc to (choose folder "Please choose where you would like to save the files") as text --Loc = Location of where directory will be placed
set newfoldername to JobName & " - " & DT & " - " & Wedding --Adds Date, Couples name and event type to make directory name
set folderStructure to "/{Audio/{Music,RAW\\ Audio\\ Cards},Documents,Exports,FCPX\\ Library,Film\\ Poster,Final\\ Delivery/{DVD,USB,WEB},Images/{Graphics,Stills},RAW\\ Cards/{Cam-1,Cam-2,Cam-3},XML}"
do shell script "mkdir -p " & quoted form of POSIX path of (loc & newfoldername) & folderStructure
set sourceFile to (path to movies folder as text) & "WeddingTemplate Creator:2005_0001.rtf"
set destFolder to loc & newfoldername & ":Final Delivery:DVD:"
tell application "Finder"
duplicate file sourceFile to folder destFolder
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

how to use applescript to move files to new folder by extension, keeping subfolder name

Here's what I'm trying to do.
I've got a file structure that contains photos in both JPG and RAW formats. It's a folder called "Photos" with subfolders by date. I'd like to copy just the RAW photos to a new folder, "Photos RAW", but keep the structure by date taken/created.
I can copy just the files using automator or applescript a directory to a new one, but how do I walk the directory tree using applescript so I cover all the subfolders?
Try this. You'll see I used "entire contents" to get the files in the subfolders too.
set extensionToFind to "raw"
set topLevelFolder to (choose folder) as text
set pathCount to count of topLevelFolder
tell application "Finder"
-- get the files
set rawFiles to files of entire contents of folder topLevelFolder whose name extension is extensionToFind
if rawFiles is {} then return
-- setup the folder where the files will be moved
set rawFolder to ((container of folder topLevelFolder) as text) & "Photos_Raw:"
do shell script "mkdir -p " & quoted form of POSIX path of rawFolder
repeat with aFile in rawFiles
set aFileContainer to (container of aFile) as text
if topLevelFolder is equal to aFileContainer then
-- here the file is at the top level folder
set newPath to rawFolder
else
-- here we calculate the new path and make sure the folder structure is in place
set thisFile to aFile as text
set subFolderPath to text (pathCount + 1) thru -((count of (get name of aFile)) + 1) of thisFile
set newPath to rawFolder & subFolderPath
do shell script "mkdir -p " & quoted form of POSIX path of newPath
end if
move aFile to folder newPath
end repeat
end tell

Resources