Using AppleScript to Select a File - macos

I'm trying to get AppleScript to select a file, but I'm getting an error when I execute the script.
Here's the code
tell application "System Events"
set a to "/Users/me/files/"
set fileName to "myFile.jpg"
set thePath to POSIX path of a
tell application "Finder"
set selection to fileName of thePath
end tell
keystroke "c" using command down
end tell
I'm getting an error "Can’t get POSIX path of "/Users/me/files/"
Essentially, what I'm trying to do is find a way to select a file so that I can copy it for later. But I want to copy the actual file, not the path of the file. The idea is to create a service that copies the file so that I can paste it into another application easily.
If there's a better way to do this, then please let me know

These following 2 lines of code would copy your file to the clipboard. This would only work with a single file. Not multiple items.
activate
set the clipboard to POSIX file (POSIX path of (choose file))

Related

How do I get the path of the container folder of a file?

I want to pass the path of the containing folder of a file to a command make new file at path. I know how to get the path of the currently open file (with tell application "TexShop" to set thePath to get path of document 1), but I don't know how to get the path of the containing folder.
I tried using container of, like this:
tell application "TeXShop"
set thePath to get container of path of document 1
end tell.
But I get this error:
TeXShop got an error: Can’t make container of path of document 1 into type reference.
Is that because while the container command is allowed in the application Finder it is not for the application TeXShop?
This following AppleScript code will get the path to the front most document in the front most application and the path to its containing folder. This will also create the new file in the containing folder.
(* The Delay Command Gives You Time To Bring The Desired App To The Front
Mainly For Use While Testing This Code In Script Editor.app *)
delay 5 -- Can Be Removed If Not Needed
tell application (path to frontmost application as text) to ¬
set documentPath to (get path of document 1) as POSIX file as alias
tell application "Finder" to set containingFolder to container ¬
of documentPath as alias
tell application "Finder" to make new file at containingFolder

Applescript error "invalid key form" when run

I tried to run this applescript:
tell application "Finder"
duplicate POSIX file contents "/Users/xx/Desktop/xxxx/" to POSIX file "/USB/" with replacing
delete POSIX file contents "/Users/felix/Desktop/xxxx/"
empty trash
end tell
but every time an error message appears saying:
Invalid key form.
You don't need to use contents. This should work.
tell application "Finder"
set src to POSIX file "/Users/xx/Desktop/test.tmp"
set dst to POSIX file "/USB/"
duplicate src to dst with replacing
end tell

AppleScript choose file or folder

can I use AppleScript to choose either file or folder in one time?
Now I could use
tell application "SystemUIServer" to return POSIX path of (choose file)
or
tell application "SystemUIServer" to return POSIX path of (choose folder)
to get file or folder. However I cannot get file or folder in one time.
No, you can't do it with "choose file" or "choose folder" verbs, but choosing a file or folder (or multiple files/folders) is supported by the underlying NSOpenPanel. So you can do it with AppleScriptObjC. Here's an example using ASObjCRunner (derived from here):
script chooseFilesOrFolders
tell current application's NSOpenPanel's openPanel()
setTitle_("Choose Files or Folders") -- window title, default is "Open"
setPrompt_("Choose") -- button name, default is "Open"
setCanChooseFiles_(true)
setCanChooseDirectories_(true)
setAllowsMultipleSelection_(true) -- remove if you only want a single file/folder
get its runModal() as integer -- show the panel
if result is current application's NSFileHandlingPanelCancelButton then error number -128 -- cancelled
return URLs() as list
end tell
end script
tell application "ASObjC Runner"
activate
run the script {chooseFilesOrFolders} with response
end tell
ASObjCRunner converts a NSArray of NSURL objects into an AppleScript list of files; the results can look something like:
{file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}
Firstly, you don't need a tell for that.
POSIX path of (choose file)
Secondly, it is not clear why you need this. Do you mean you want to select a file and it's folder? That's not how you do it; you select the file then parse the file path for the containing folder or use one of the many methods to do that, like
set f to (choose file)
set posixF to POSIX path of f
tell application "Finder" to set filesDir to container of f as alias as text
set posixDir to POSIX path of filesDir
{f, posixF, filesDir, posixDir}
If you want to be able to select multiple folders and files at the same time, I don't think there is a "pure applescript" way to do this (aside from using a drag-drop aware script application).

Open a local file with applescript within application

How would I go about opening a file from within an applescript application? I'd place it in /Contents/Resources of my applescript application. What I want to know is what I would tell the actual script to do?
To get the path to your application, use the path to me command and build a path to your resource. Then, you can use Finder to open the file with the default program, or you can tell a specific program to open the file.
set filepath to (path to me as string) & "Contents:Resources:file.ext"
tell application "Finder"
open alias filepath
end tell
--OR
tell application "Microsoft Word"
open alias filepath
end tell

move a file in finder with applescript

I just want to move an image from one folder to the other, replacing the one that's already in there:
tell application "Finder"
copy file "/Users/xx/Documents/img.jpg" to folder "/Users/xx/Documents/State"
end tell
When I run it, I get an error message saying
Finder got an error: Can’t set folder [path] to file [path]"."number
-10006 from folder [path]
Please help me!
Try:
tell application "Finder"
duplicate POSIX file "/Users/xx/Documents/img.jpg" to POSIX file "/Users/xx/Documents/State" with replacing
end tell
Or
tell application "Finder"
move POSIX file "/Users/xx/Documents/img.jpg" to POSIX file "/Users/xx/Documents/State" with replacing
end tell
As #adayzdone notes, the error appears because you're using a Posix-style path without declaring it.
Another approach is to use colon-separated HFS paths, like so:
move file "Macintosh HD:Users:xx:Documents:img.jpg" ¬
to "Macintosh HD:Users:xx:Documents:State:" with replacing
With colon-separated paths you need to include the whole thing, including the volume name (I'm assuming Macintosh HD here), otherwise it'll throw our good friend error 10,006.
It helped me:
set theSource to POSIX file "/Users/xx/Documents/img.jpg"
set theDest to POSIX file "/Users/xx/Documents/State"
tell application "Finder"
move theSource to folder theDest with replacing
end tell

Resources