How to reference a file on users desktop with Applescript - applescript

I'm pretty new to Applescript and am trying to reference a file on the currently logged-on user's desktop, but I don't know how. In POSIX you'd use the tilde character to specify the user's home folder, but that doesn't seem to work in applescript. I've read other posts about how "desktop" is already defined as a keyword applescript understands, but I don't know how to specify a sub-folder of the pre-defined keyword (desktop/folder doesn't work).
Any help would be appreciated.

set theTargetFolder to ((path to Desktop Folder) & "name of target folder") as string
returns "Macintosh HD:Users:username:Desktop:name of target folder"

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

Apple Automator take screen capture and save to user-specified location

So, I'm trying to use Automator under Mac OS Yosemite to create a service to allow a user to take a screenshot and save it to a location they specify, through some sort of "Save As" dialog. It seemed like it should be easy, but for some reason I'm running into difficulty with it. The screenshot component is easy, using the "Take Screenshot" action in Automator, but it's the saving it to a custom location that's causing me problems.
After trying a few different approaches, it seemed the easiest thing to do was to save the screenshot to a fixed directory/filename from within the "Take Screenshot" action, and then (using AppleScript) rename it in that directory, and move it to the user-specified target directory. So, I added a "Run AppleScript" action to my service. In it, I generate the dialog to choose a file name/path, using the choose file name command in AppleScript. I'm trying to split up the file name from the path, so that I can rename the file I save in "Take Screenshot," and then move it to the path that I'd like to save it at. I can get the full path, but am having problems just getting the filename from the path—and I've tried a variety of suggestions from what I've seen online. In my screenshot, the error shown was from attempting to do
I'm not set by any means on this flow, so if anyone has any better suggestions on how to do what I'm trying to do, by all means please let me know. Otherwise, if someone's able to just tell me how I can extract the filename from the path (and also if there's some special way you have to use that string to rename the file) that'd be great!
AppleScript code pictured in screenshot:
on run {parameters}
set thePath to (choose file name with prompt "Where would you like to save your file?")
tell application "Finder"
display dialog thePath as string
end tell
set UnixPath to POSIX path of (thePath as text)
display dialog UnixPath
end run
I tried this but it didn't work:
set basePath to POSIX path of (parent of (thePath) as string)
Thanks for checking it out!
An easy way would be to use the command line tool "screencapture". It has many options you can choose. See its man page. Here's an example that you can run as an applescript directly or you could put this inside an applescript automator action if you want.
Good luck.
set thePath to (choose file name with prompt "Where would you like to save your file?")
do shell script "screencapture -mx -T1 " & quoted form of (POSIX path of thePath & ".png")
I've created a Folder action in Automator. Choose Desktop. Add Find Finder objects (search: Desktop). Add Move Finder Object and choose your preferred destination. This will automatically move all your screenshots.

moving one folder to another folder - applescript

I'm trying to move a user-selected folder to another folder, but I don't get it: whenever I look it up, it looks too complicated, even though I know it's not supposed to be.
Here's the code I have so far - please help:
choose folder with prompt "Select folder:"
on open of finderObject
tell application "Finder" to move (finderObject) to folder "Library:Application Support"
end open
end
First off, you have to assign the chosen folder to a variable.
Also, many special folders in the OSX file structure have special names to get their paths, including the library and the application support folders. So, your script can simply be:
set finderObject to choose folder with prompt "Select folder:"
tell app "Finder" to move finderObject to folder (path to Application Support folder from local domain as string)
However, that is the root level Library folder. I suspect you will want to use the Application Support folder in the ~/ user domain. For that, change the "from local domain" to "from user domain".

Auto scripting two folders with the same name, but 1 folder appended with a prefix

I’m new to the forum and relatively new to scripting and wanted to ask a question.
I’m trying to script a workflow where when prompted, users create a folder and when the script executes, a dialogue pops up. Users name the folder, for example; NewFolder1, it is then saved in a directory. Another folder is then created in the background, into another directory with the same name created by the user earlier, but appended with a prefix, for example; Name_ NewFolder1.
I then want sub folders created in each directory as well as alias folders which point to Name_ NewFolder1 created in the original (NewFolder1) directory
I'm on a mac and I can do most of this via applescript or automator but am stuck on the initial creating 'NewFolder1' and 'Name_ NewFolder1' at the same time in the two separate directories.
I only want to prompt the user to name one folder rather than show them two dialogues for 'NewFolder1' and 'Name_ NewFolder1'
Does anyone have any ideas of how i can achieve this? Either via a shell, applescript or UNIX
Thank you
Here's how to do the part you're stuck on in AppleScript:
set folder_name to text returned of (display dialog "What is the folder name?" default answer "untitled")
set second_name to "Name_" & folder_name
tell application "Finder"
make new folder at desktop with properties {name:folder_name}
make new folder at desktop with properties {name:second_name}
end tell
`set client_name to text returned of (display dialog "Create Folders:" default answer "Type here")
set loc1 to ":path1"
set loc2 to ":path2"
set clientmedia_name to "name_" & client_name
tell application "Finder"
make new folder at loc2 with properties {name:clientmedia_name}
set new to make new folder at loc1 with properties {name:client_name}
make new folder at new with properties {name:"1"}
make new folder at new with properties {name:"2"}
make new folder at new with properties {name:"3"}
make new folder at new with properties {name:"4"}
end tell`
This is how far i have got, but can't seem to create folders in the clientmedia_name dir
Thanks,

Resources