AppleScript copy and paste file with right-click - applescript

Sorry I don't have more of a foundation but I know nothing of AppleScript. Basically I want to right-click on a file or folder and run a script that will copy it to a new location with a constant directory structure. So obviously the scripting is way off, but something like this..
path = /Volumes/RENDERS/ThisShow/ThisShot/ThisShow_ThisShot_v10/
newPath = from 'path' ReplaceText("/Volumes/RENDERS/" , "/Volumes/Raid-Renders/")
tell application "Finder" to duplicate file 'path' to 'newPath' with replacing
In this example "path" would be the file or folder that was right clicked on when launching the script. The new folder in this example would be "/Volumes/Raid-Renders/ThisShow/ThisShot/ThisShow_ThisShot_v10/". So the idea is that it would copy "ThisShow_ThisShot_v10" folder and its content to "/Volumes/Raid-Renders/ThisShow/ThisShot/"
Thanks for any help. I know there isn't a lot to go on here.

This should do it:
set thisFolder to the POSIX path of (choose file)
set the destinationFolder to (replaceText(thisFolder) as POSIX file as alias)
tell application "Finder" to duplicate (thisFolder as POSIX file as alias) to the destinationFolder with replacing
on replaceText(this_folder)
set AppleScript's text item delimiters to "RENDERS"
set these_items to every text item of this_folder
set AppleScript's text item delimiters to "Raid-Renders"
return these_items as string
end replaceText
As far as I know, you can't assign an AppleScript to a right-click menu. However, you can create a menu bar script. To do this, first save this script as a regular script file in the Scripts folder of the local Library folder.
If you have a little icon in your main menubar (located at the top of the screen) that looks like a scroll (formerly known as the Script Menu), the script should appear somewhere in that menu. If you don't see the icon, run AppleScript Utility (located at /Applications/AppleScript/AppleScript Utility) and check the Show Script Menu in menu bar checkbox.
Now, all you have to do to run the script is open up the Script Menu, find your script, and just click on it once. Questions? Ask. :)

Related

MacOS Terminal: Is it possible to create a "drag-and-drop" action via script?

There are many posts that explain how to drag-and-drop things into an open Terminal window. But what I would like to do is to tell Terminal to drag-and-drop a previously selected directory onto another application like VSCode or Renamer. I have not found any documentation for that. Is it at all possible? And if so, would somebody please point me to a documentation?
UPDATE:
I'd like to clarify my question with what I intend to do:
Pre requisites:
a "work folder" contains folders and files that shall be renamed
the renaming is done by an application called "A better finder renamer" (which allows presets)
An "Automator" (MacOS app) action shall imitate these steps:
the "work folder" is right clicked
the folder is drag-and-dropped onto the ABFR, which initiates the currently active preset
other actions via bash (like 'mv .//.* ./') ...
It is the "drag-and-drop" part of the Automator action that presents a riddle for me.
The "drag-and-drop" operation is manual operation. In AppleScript, instead the command to open the file or folder is given to the destination application itself.
Second thing to keep in mind. Getting Terminal's current working directory is easy with its do script "pwd" command. But the result of the do script Terminal command returned to the script is always the window tab, not the result of the pwd shell command. One solution is to redirect (remember) the result of pwd in a temporary text file.
set tempFolder to (path to temporary items folder from user domain)
set temp to POSIX path of tempFolder & "workingDirectory.txt"
tell application "Terminal" to do script ("pwd > " & temp) in selected tab of window 1
set curDirPosixPath to paragraph 1 of (read file ((tempFolder as text) & "workingDirectory.txt"))
set curDirHFSPath to curDirPosixPath as POSIX file as Unicode text
tell application "Visual Studio Code" to open curDirHFSPath
.
NOTE: other possible solution (I don't like) is parsing the text contents of Terminal window after pwd command execution. You can get contents using property contents (of selected tab of front window).
Open Automator, choose create New Document.
Choose create new Quick Action (service).
Set workflow receives current folders in any application.
From library Add Run AppleScript action. Edit it contents:
on run {input, parameters}
set curDirHFSPath to (item 1 of input) as text
tell application "Visual Studio Code" to open curDirHFSPath
end run
Save this Quick Action as service. Now when right-clicking the folder, opens the context menu, where you can choose and run this service.

Choose file prompt, show hidden files? Applescript objtc

I use this to get a prompt where i can select a file
set thePath to quoted form of POSIX path of (choose folder with prompt "whatever)
How can i see hidden files in the "choose folder with prompt"?
Allowing to see hidden files in finder doesn't do anything different.
You simply need to add with invisibles:
set thePath to quoted form of POSIX path of ¬
(choose folder with prompt "whatever" with invisibles)
You can find the full syntax of the choose folder command in the Standard Additions dictionary: in AppleScript Editor, press Cmd-Shift-O (or select File > Open Dictionary...) and select StandardAdditions.osax from the list.

is it possible to open a folder in Finder in the *current* space from the command line?

i want to issue a terminal command (invoking AppleScript is fine) to open a path in a Finder window in the current space.
if the path is open in Finder in another space, i don't want to switch to that space or move that finder window to the current space. if the path is already open in a Finder window in the current space, either focusing on it or opening a new Finder window to it would be fine.
i run a lot of spaces that are setup for different tasks, and i don't want to remove the Finder window from any other space i might be using it or switch to any other space.
In Applescript, for example opening the Applications folder: -
tell application "Finder"
make new Finder window to folder "Applications" of startup disk
end tell
You can save that as a script and call it from the command line with the osascript command, or you can execute it directly with the -e argument to osascript: -
osascript -e 'tell application "Finder" to make new Finder window to folder "Applications" of startup disk'
If you would like to open a specific path, instead of a named folder, you can reference the path with this:-
POSIX file "/some/directory/path"
So, a complete script would be: -
tell application "Finder"
activate
make new Finder window to (POSIX file "/some/directory/path")
end tell
Note: the activate command focuses the Finder window, which is optional.

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).

Accessing folders in AppleScript

Sorry, complete beginner here with AppleScripting.
I'm trying to do a very simple thing, move files from one folder to another.
tell application "Finder"
set this_folder to "Users:chris.nicol:Movies:SmartConverter:"
set this_list to every file of this_folder
repeat with i in this_list
--if i does not start with "x" then
move i to "users:chris.nicol:music:itunes:itunes media:automatically add to itunes:"
--end if
end repeat
end tell
However I keep getting an error:
I've tried different commands (count, parentContainer, etc.) on the folder, but I get the same type of error. I've tried different formatting for the folder ...
Users/chris.nicol/Movies/SmartConverter/
Macintosh HD:Users:chris.nicol:Movies:SmartConverter
etc.
Any ideas on what I'm doing wrong?
Thanks in advance,
Chris
Simple tip... if you want to find the proper path you should use try this and look in the results field for the proper path. You'll see that the name of the hard drive, Macintosh HD, is required. Note you could use "choose file" as well if you wanted the path to a file.
(choose folder) as text
Next, the path that you will see is a string. Applescript sees it as a string not a path to a file or folder. As such, when you want to use the string as a path then you must put the word "file" or "folder" in front of it as appropriate to make applescript use it properly. Therefore your script should look like this. Note that the move command can handle a list of files so the repeat loop isn't needed.
set this_folder to "Macintosh HD:Users:chris.nicol:Movies:SmartConverter:"
tell application "Finder"
set this_list to every file of folder this_folder
move this_list to folder "Macintosh HD:Users:chris.nicol:music:itunes:itunes media:automatically add to itunes:"
end tell
Try:
set thisFolder to (path to movies folder as text) & "SmartConverter"
set thatFolder to (path to music folder as text) & "itunes:itunes media:automatically add to itunes"
tell application "Finder" to move files of folder thisFolder to thatFolder
You must have some invisible or otherwise uncopyable files in one of the folders. Either add something like without invisibles to the end of your set this_folder line, or get rid of your loop altogether and simply call
move files of entire contents of this_folder to (the Add Automatically folder)

Resources