Listing folder in /Users - applescript

Using Applescript, I have been able to successfully list the folders on my desktop as a selection using this code:
set the_folder to (path to desktop)
tell application "Finder"
set foldernames to name of every folder of entire contents of the_folder
end tell
set theChosenOne to choose from list folder names
However, when I attempt to do the same for the /Users folder:
set Users to "/Users"
set the_folder to Users
tell application "Finder"
set foldernames to name of every folder of entire contents of the_folder
end tell
set theChosenOne to choose from list foldernames
It returns this error: error "Can’t get entire contents of \"/Users\"." number -1728 from «class ects» of "/Users"
Searched on that error, but not finding much info. Thanks for any help you can give me with this one.

The error occurs because the Finder does not support slash separated POSIX paths.
But there is a simpler solution. path to users folder return an alias reference to the folder /Users which can be used directly.
set the_folder to path to users folder
tell application "Finder"
set foldernames to name of every folder of entire contents of the_folder
end tell
set theChosenOne to choose from list foldernames
Caveat: Be aware that entire contents is very slow. After 2 minutes you will get an Apple Event timed out error. You might wrap the Finder tell block in a with timeout block. However I'd recommend find or mdfind of the shell which are incredibly much faster. And most likely you will get also an access privileges violation error.

Related

Unable to find the application support folder for a specified application inside an Apple Script

I have an apple script that asks the user to select an application as a prompt. I would like to know how it could be possible to find the directory of the Application Support folder for this application per example, Spotify is
~/Library/Application Support/Spotify/
On my mac, but like is there a way inside apple script to find this directory for the specific app. It would really be appreciated, thanks!
Because I would like to then delete this folder using Apple Script
This should accomplish what you're looking to achieve. Be careful though because using System Events to delete... deletes the item permanently instead of sending it to the Trash
property folderNames : missing value
tell application "System Events"
set appSupportFolder to application support folder
set folderNames to name of folders of appSupportFolder
set theChoice to my chooseFolderToDelete()
delete folder theChoice of appSupportFolder
end tell
to chooseFolderToDelete()
activate
set theChoice to (choose from list folderNames with prompt ¬
"Select an Application's Support folder to delete" OK button name ¬
"Delete Folder" cancel button name "Cancel") as text
end chooseFolderToDelete

Issue Moving Files in AppleScript

I'm having trouble with the following line of AppleScript:
move file currentFile to POSIX file "/Users/UserName/Desktop/FolderName"
I've found that it works in one script but not in another, and I can't figure out why. In both scripts, currentFile is defined as an item in a list.
I've been trying different workarounds for hours but I can't get anything to work. Is there any reason why this code would work in one circumstance but not in another?
Thank you!
AppleScript doesn't know how to move a file.
It has to ask the Finder or System Events. In case of the Finder the syntax is pretty easy because the desktop folder of the current user is the root folder of the Finder
tell application "Finder"
move file currentFile to folder "FolderName"
end tell
currentFile must be an HFS path (colon separated).
In case of System Events the syntax is slightly different and System Events supports also POSIX paths
tell application "System Events"
move file currentFile to folder "FolderName" of desktop folder
end tell

Granting permissions to AppleScript script

I've a simple AppleScript that it's supposed to delete everything in the Downloads folder with exception of selected list of folders passed at the end.
tell application "Finder"
delete (every item of "Downloads" of folder currentUser of folder "Users" of startup disk whose name is not in {"PreciousFolder"})
end tell
On save the AppleScript returns the following error:
You are going to delete every item of the literal string "Downloads" which makes no sense. Add the folder specifier
tell application "Finder"
delete (every item of folder "Downloads" of folder currentUser of folder "Users" of startup disk whose name is not in {"PreciousFolder"})
end tell
or shorter
tell application "Finder"
delete (every item of (path to downloads folder) whose name is not in {"PreciousFolder"})
end tell

How can I open an MP3 with quicktime player using applescript?

I have the following code to open the files, but they all open with iTunes:
tell application "Finder"
set the_files to get every file of folder ("Macintosh
SSD:Users:myusername:Desktop:DJ:Music:Sort")
end tell
repeat with theItem in the_files
tell application "QuickTime Player"
open theItem
end tell
end repeat
Thanks!
AS syntax can be very misleading. While your open command is inside a tell application … end tell block, that doesn’t necessarily mean it will be sent to that application. If the command’s direct parameter happens to be a reference to objects in another app (in this case, a reference to a document file object in Finder) AS will send it to that app instead.
Use set the_files to get every file of folder (…) as alias list. That will get you a list of AppleScript alias values, instead of a list of Finder references (which any app other than Finder wouldn’t understand anyway).

How do I Copy files to a USB with apple script that runs on said USB?

I run a computer lab at Denver University. We are having an issue with our backup system, and I need to copy/duplicate all of the files on the (Apple) computers onto a USB that I keep at my desk.
At the moment, I need to plug in a USB into each of the computers and manually duplicate each of the items of the "documents" folder and deposit them into a new folder on my USB. I need an Apple Script that I put on my USB, insert my USB into a computer, activate the app, and it will make a new directory named "(User name) upload", and deposit all of the items into that directory. Here is what I have so far:
tell application "Finder"
set theFolder to disk / Volumes / Lexar / stuff
set Files1 to Users / matanya / documents
tell application "Finder"
try
duplicate file Files1 to theFolder
on error
tell application "Finder"
display dialog "Transfer Failed"
end tell
end try
end tell
one of the issues is that every time I run the script, I get an error that says the the variable "Volumes" is not defined. Another one is that I am afraid that when I plug this script into another computer, it will not find the folder "matanya" that I have in my directory. Is there a way to call it "home" or something?
First of all the Finder accepts only HFS paths – starting with a disk name and colon separated. The folder Volumes doesn't matter when using HFS paths.
path to documents folder points always to the folder Documents of the current user.
set theFolder to "Lexar:stuff:"
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
try
duplicate documentsFolder to folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
More sophisticated this version creates a folder with the short user name of the current user and duplicates the documents folder to that individual folder
set theFolder to "Lexar:stuff:"
set currentUser to short user name of (system info)
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
if not (exists folder currentUser of folder theFolder) then
make new folder at folder theFolder with properties {name:currentUser}
end if
try
duplicate documentsFolder to folder currentUser of folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
With launchd you even could run this script automatically whenever the USB drive is inserted.

Resources