Issue Moving Files in AppleScript - 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

Related

Can't set Desktop Background on Mac with AppleScript

I am trying to set the Desktop Wallpaper of my Mac(Running latest version of Catalina). But I keep getting this error message when trying to run my apple script.
error "System Events got an error: Can’t set file \"Library:Desktop Pictures:Ink Cloud.jpg:\" of current desktop to file \"Library:Desktop Pictures:Ink Cloud.jpg:\" of current desktop." number -10006 from file "Library:Desktop Pictures:Ink Cloud.jpg:" of current desktop
here is my code
tell application "System Events"
tell current desktop
set picture rotation to 0
set picture to file "Library:Desktop Pictures:Ink Cloud.jpg:"
end tell
end tell
I've been able to change all the other properties of the Desktop except the actual photo. I also tried using / for the file path. I have tried different file paths. But still not luck. Any help or advice would be greatly appreciated.
Running the code
tell application "System Events"
set currentPicturePath to picture of current desktop
end tell
reveals that the path is supposed to be a (slash separated) POSIX path
tell application "System Events"
tell current desktop
set picture rotation to 0
set picture to "/Library/Desktop Pictures/Ink Cloud.jpg"
end tell
end tell
Your (colon separated) HFS path contains two mistakes:
An HFS path (unlike the POSIX path) starts always with a disk name.
Only references to folders and packages have a trailing colon.

Listing folder in /Users

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.

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.

Creating an automator service to create a new document in the current directory

so I'm trying to create a service that will be located in the contextual menu of the Finder and that would allow to create a new document in the current directory.
I've been doing that using Automator:
Sorry everything's in French ^^
Anyway here's the AppleScript that I'm using to retrieve the current working directory:
on run {input, parameters}
tell application "Finder"
set pwdAlias to insertion location as alias
if not (exists folder pwdAlias) then
set pwdAlias to (container of pwdAlias) as alias
end if
end tell
set pwd to POSIX path of pwdAlias
return pwd
end run
Then I'm setting this value to a variable, then creating a new text document using the variable as the path for the document and finally I'm using the command Reveal in Finder to show the created document.
Everything's is working fine except that the script seems to always be late!
What I mean is that when I open a new Finder window and select my service, it is systematically creating the document on the previous window as shown below:
But then if I try a second time, the document is being created properly at the expected location:
And this is very systematic it happens every time!!
Sorry if I'm not very clear, it is not so easy to explain!
Well otherwise, I'm running Mountain Lion and here's the Automator project attached: create_new_document
To add the service just unzip and put the file under ~/Library/Services/
Hope to get some answers but I fear that this is just an Automator bug!
Try this
Depending on what you want to be clicking.
Set the Services selected to: 'folders'
or files or folders. in 'Finder.app'
Get first Finder Window path Action
You can download the Get first Finder Window path Action from my blog post here
The download is at the bottom of the post.
The Action gets the posix path of the frontmost finder window.
Since you are clicking on a folder in a window. that window will be the one returned.
Set Value of Variable
Get Specified Text
The next action 'New Text File' needs some input. If it does not get any, no file will be created. You can leave the text field blank. Just having the action in place works.
New Text File
Drag the Variable 'path' or what ever you named it on to the Where: drop down menu.
you can click the double blue lines at the bottom of the Automator window to toggle the workflow Variable List
Save your service. And try it.
(It may take a short while to show up in the contextual Menu.)
It's an open bug in 10.7 and 10.8
Use this Workaround
on run {input, parameters}
activate application "System Events"
activate application "Finder"
tell application "Finder"
set pwdAlias to insertion location as alias
set pwdAlias to (container of pwdAlias) as alias
end tell
return POSIX path of pwdAlias
end run

Applescript simple open save file snippet?

I just need a snippet to open and then save a file. It's an illustrator PDF with linked artwork, problem being the linked files do not update unless I open then save the file and I am having to do this sooooo many times it's making my brain explode.
Should be straightforward. I tested the following snippet in Illustrator CS3 (13.0.2) on Mac OS X 10.6.8.
set theFiles to choose file with prompt "Select the files" of type {"pdf"} with multiple selections allowed
tell application "Adobe Illustrator"
repeat with theFile in theFiles
open theFile without dialogs
save current document in theFile as pdf
end repeat
end tell
The choose file command (which prompts the user to locate one or more files) is just one way of obtaining a list of files. You could just as well use
tell app "Finder" to set theFiles to every document file of folder "Macintosh HD:Full:Path:To:Your:Folder" whose file type begins with "pdf"
if you want to get all PDF files inside a specific folder.

Resources