Im wondering how to copy a folder that resides in the applescripts running directory to a specific folder on the MAC, making sure to merge the files/folders inside?
Cheers
K
set The_Moving_Folder to "Macintosh HD:Users:ComputerName:Desktop:Test1"
set The_Overwrite_Move_Location to "Macintosh HD:Users:ComputerName:Desktop:Test2"
do shell script "mv " & quoted form of (posix path of The_Moving_Folder) & " " & quoted form of (posix path of The_Overwrite_Move_Location)
The mv command will move directory paths from point A to point B. Not sure if this is exactly what you're looking for because you have no code up. However based on the question and comment, this should do the trick. If you're looking at moving a file to a folder than you want the cp command.
For more data about these commands, run man command_name in terminal.
Related
I'm using the newest version of macOS Monterey.
Sometimes I have to merge/combine all files that are in a specific folder into one txt file.
I currently do that by typing this in Terminal:
cd /Users/my_name/Desktop/test_folder ; cat * >merged.txt
This will merge/combine all files in folder test_folder into one file called merged.txt. The file merged.txt will get saved into the folder test_folder.
Every time I need this I have to open Terminal copy/paste the command and replace test_folder with the right folder name, since it's not always the same.
I want to make this easier by just make a right click on a folder, go to Quick Actions and select e.g. Merge all files to merge/combine all files inside the folder I just clicked on.
But I stuck at getting the folder name. How can I dynamically get the folder name and path I clicked on to start this Quick Action instead of the hard coded /Users/my_name/Desktop/test_folder?
Or, is there another and easier solution?
This is what I have so far:
I wouldn’t do this with AppleScript, especially if all it’s ultimately doing is calling out to a shell script.
Stick with the Run Shell Script action except change the option for passing the input as arguments rather than to stdin.
The folders selected in Finder will then be available to your script via $#, so you can do something like:
for d in "$#"; do
cat "$d"/* > "$d/merged.txt"
open -R "$d/merged.txt"
done 2>/dev/null
This loops through the selected directories and concatenates the files to merged.txt in the respective directory. The open -R line reveals the merged.txt file in Finder.
Errors are written to /dev/null, i.e. discarded, as cat will throw an error if any of the directories, themselves, contain directories.
Instead of adding a Run Shell Script to your workflow, try adding a Run AppleScript command instead. Copy this following AppleScript code to the Run AppleScript command.
on run {input, parameters}
try
do shell script "cd " & quoted form of POSIX path of input & " && cat *.txt > merged.txt"
on error
try
do shell script "cd " & quoted form of POSIX path of input & " && rm merged.txt"
end try
end try
end run
I'm trying to create an app to transfer any files with a certain name a different folder. So far, I have the following:
tell application "Finder"
move (every item of (get path to home folder) whose name is "extended image name.jpg") to ((get path to home folder) & "Pictures" as string)
end tell
While this doesn't return any errors, it doesn't do what I want it to either. I am also aware that this only searches the home folder, so if there is any way to do a broader search of the whole drive without having to enter a username, that would be great (I want this to be able to run on more computers than one without them having to edit the script.)
-Thanks
The code considers only files in the home folder but not in its subfolders, to consider all subfolders you have to add entire contents
tell application "Finder"
move (every item of entire contents of home whose name is "extended image name.jpg") to folder "Pictures" of home
end tell
But be aware that entire contents is extremely slow. The shell command find or a spotlight search with mdfind is much faster for example
set homeFolder to POSIX path of (path to home folder)
set picturesFolder to POSIX path of (path to pictures folder)
do shell script "mdfind -onlyin " & quoted form of homeFolder & " -0 'kMDItemDisplayName = \"extended image name.jpg\"' | xargs -0 -J {} mv {} " & quoted form of picturesFolder
Important Note:
As you are moving multiple files with the same name the Finder version will ask for overwriting and the shell version will overwrite all files with the same name.
When you use the Finder to search, if your folder contains only few hundreds files that's OK. But if you want to search in a folder containing thousands of files, Finder will take too long. In this case, it is better to use the shell command 'find' which is much faster.
Syntax of find command is : find directory/ -name target_file_name
Even more, you can chain that command with -exec fonction which will used result of the find to do something: in your case to copy files found in Pictures folder.
In -exec command the {} means the file found. The shell copy command is cp.
This ends with : find /Users/myUserName/ -name 'extended image name.jpg' -exec cp {} \;
(note: the \; tells the system that's the end of the -exec command)
Overall you can run this command in Applescript via do shell script :
set Source to POSIX path of (path to home folder)
set Dest to POSIX path of (path to pictures folder)
set TargetName to "extended image name.jpg"
set BackSlash to ASCII character 92
set SemiCol to ASCII character 59
try
do shell script "find " & Source & " -name " & quoted form of TargetName & " -exec cp {} " & Dest & " " & BackSlash & SemiCol
end try
It is much longer than Finder syntax, but also much faster to run !
Notes:
1) the POSIX path convert the Finder path with : to shell path with /
2) the Backslash and SemiCol are set to \ and ; . this is a work around to avoid that Applescript compiler misinterpret the \ during compilation
3) the do shell script is in try/end try block to avoid error. "Find" gives errors when you try to access to files without permission. Such errors will be ignored.
4) with this method, you can replace Source by "/". Doing so, the find will search in all directories of the main drive (it may take some time !). If you want to search for all users, set Source to "/Users/"
I want to make a little script to compress folder. In fact I want to do exactly what is done when you right clic on a folder and select "compress": the zip file has the same name as the folder and when you clic on it, it creates back the folder and the files.
Here is my code:
set the_src to "Mac Info:Users:Maison:Desktop:Dest_So36"
set the_src to quoted form of POSIX path of the_src
set the_dest to "Mac Info:Users:Maison:Desktop:Dest_So36.zip"
set the_dest to quoted form of POSIX path of the_dest
do shell script "cd " & the_src & ";zip -r " & the_dest & " *"
If I use zip -r it takes all sub folder and file from the orginal folder but when unpacking, it give back the content of the original folder (subfolder and files) but not the main folder itself.
It I use zip -jr, it loose all the sub-folder (it get the files but not the folder).
Thanks
Changing the directory is fine. You don't need the asterisk.
do shell script "cd '/Users/Maison/Desktop/'; zip -r 'zip-a-dee-doo-dah.zip' 'Dest_So36' "
Why are you using cd (changedirectory) to go INTO the folder?
When you are in the parent folder, you can use the zip -r command and it will work.
So I believe you command should become:
do shell script "zip -r " & the_dest & ".zip" & the_dest
I have a script that moves files to Trash, but I work with tens of thousands of files so I need it to do fast. Current script gets Finder stuck and I need to relaunch Finder every time I use the script.
set source_folder to POSIX path of (path to pictures folder) & "4K Stogram"
do shell script "/usr/bin/find " & quoted form of source_folder & " -type f -exec /bin/mv {} ~/.Trash \\;"
display notification "All images were processed." with title "New" sound name "Glass.aiff"
tell me to quit
Wouldn't the shell script's 'rm -f' command make it faster?
How the script should look like if yes?
I don't need those files in Trash anyway, I delete them right after the script finishes the job.
Add the -delete option to the find command.
Like this:
set source_folder to POSIX path of (path to pictures folder) & "4K Stogram"
do shell script "/usr/bin/find " & (quoted form of source_folder) & " -type f -delete"
You should be able to pipe your find results to the rm command. Understand that although rm should be faster, it is permanent. Be sure that your code is correct before invoking it.
I have this shell script which resolves a symbolic link (symlink) to a folder and copies it to the current working directory:
cp -R `grealpath /some/dir/symlink_to_folder` .
The command grealpath just resolves a symbolic link when coreutils are installed on Mac OS X.
My question is: What would be the equivalent as an AppleScript? The workflow would be ideally like this: Copy one or more symbolic link-folders in Finder to the clipboard, then rightclick on another folder in Finder -> services -> run the newly created script to copy all symlinks from the clipboard to the other folder.
You could create a service like this:
cp -R "$(/usr/local/bin/grealpath "$(osascript -e 'POSIX path of «class furl» of (the clipboard as record)')")" "$1"
If the clipboard contains references to files, «class furl» of (the clipboard as record) returns a file object for the first file.
You might also use cp -RH source target instead of cp -R `grealpath source` target. If the symlink and its target have different names, it uses the name of the symlink though.
-H If the -R option is specified, symbolic links on the command line
are followed. (Symbolic links encountered in the tree traversal
are not followed.)
Applescript will treat a symlink like it's just another folder, so all you have to do is duplicate the contents.
Tell application "Finder"
repeat with i from 1 to (count symlinks)
set newFolder to make new folder at destFolder with properties {name:item i of symlinks)
duplicate entire contents of item i of symlinks to newFolder
end