How to add text to existing clipboard - applescript

I have an automator script to create a new folder with subfolders in my current directory.
For example "20200319_video" with one of the subfolders being "Project Files"
I'm able to copy the directory of "user/downloads/20200319_video" to my clipboard, but I would like to add "/Project Files" to the end of it.
Is there any simple way to append text to the clipboard using AppleScript?

Like this:
set the clipboard to (get the clipboard & "/extra bit")

Related

Applescript command to move files in current folder with *.extension to the folder with Name inside this folder

I'm newbie in Scripting. Can you please help with this:
I need to move files with *.extension in current (selected) folder to the folder with Name inside this current (selected) folder.
for example: Move all .zip files in Folder 1, to the folder ZIP inside Folder 1. But I need to use it as a service (something like: select Folder 1, control+click "Move all zip to ZIP")
Thanks guys!
Updated Answer - More automated and with a context menu/item
You can add a context item to Finder's menu (Finder > Services), which will only appear when you have a folder selected (the one you want to move the files from.)This is what you'll see when you have a folder selected:
Follow these steps
1. Open the "Automator" application. (command + spacebar & type ""Automator" to find)
2. Press "New Document" (bottom left corner), if this doesn't show just do command + n
3. Select "Service" (One with the gear icon) and press enter.
4. Select these options in the top bar, so it looks like this:
In the search field, type in "Run AppleScript"
Drag it into the blank space on the right:
7. Delete all the script in there and paste in my script instead:
on run {input, parameters}
set fromFolder to item 1 of input
-- Choose the file extension to be moved
set fileExtension to text returned of (display dialog "Please enter the file extension you want to move." default answer "zip")
-- This will put the files in a folder inside your selected folder with the name of the file extension
-- e.g. move txt files in Kamil:Desktop:TextStuff will move it into Kamil:Desktop:TextStuff:txt
-- Change it to what suits your needs
set toFolder to fromFolder & fileExtension as string
-- Finder will handle the moving of files
tell application "Finder"
set filesMoved to (every file of folder fromFolder whose name ends with fileExtension)
move filesMoved to folder toFolder
display notification "Into \"" & toFolder & "\"" with title "Moved " & (count of filesMoved) & " " & fileExtension & " files"
end tell
end run
7. Press command + s and give it the name you want and press save.
8. Now quit Automator
Now open a Finder window and select a folder with a folder created inside it named the file extension you will be sorting; go to Finder > Services > [What you named it],type in the file extension and press enter.You should see a notification when it's done moving the files, which tells you how many were moved and to which folder.
I hope this better answers your request, and if does, don't forget to mark it as the answer, otherwise, comment on it and l'll see what I can do.

ApplescriptObjC Path To Folder

I am currently setting up an ApplescriptObjC application. Whenever I try other methods, it screws up. I'm trying to set it up where a shell script uses the mv command to move a file from the "Files" directory to the /usr/bin/ folder. I think it would go a little something like: do shell script "sudo mv " & path & "/Files/ /usr/bin/" where path would be the path to me. I have tried the path to me and posix and other stuff, just doesn't work. ![The Files folder contains a file I want to move to /usr/bin] Image of where the folder is: http://i.stack.imgur.com/jUX6w.png
First of all the folder Files in your screenshot is a virtual folder in Xcode (yellow).
You have to create a real folder (blue). The easiest way is to drag a folder in Finder to the Xcode sidebar and select "Create folder references"
To use sudo in AppleScript append with administrator privileges to the do shell script line. You will be prompted to enter an admin password.
This code moves(!) the files to /usr/bin. If you want to copy (duplicate) the files use cp -r instead of mv
set filesFolder to (current application's NSBundle's mainBundle()'s resourcePath()'s stringByAppendingPathComponent:"Languages") as text
tell application "System Events" to set filesToMove to name of files of folder filesFolder
repeat with aFile in filesToMove
do shell script "/bin/mv " & quoted form of (filesFolder & "/" & aFile) & space & "/usr/bin/" with administrator privileges
end repeat

Apple Script - Selecting files, exclude type, zip it

I need to zip the content of a folder (and all the subfolders) for hundreds of folders.
Is it possible to run a command that takes all the files of a specific folder (prompt), except all the files that have a .fla extension and zip this content into one zipfile?
Right now I am copying the folder, search for all the .fla-files, then select all the files inside the folder (I have the to zip the content, not the folder) and create a zip of it (takes way too long.
I know that it is possible to use Apple Script to delete and copy files. But does this also work in the above mentioned order + zipping?
Alright, so I was still kind of stuck with this issue.
I created a Bash Script, that is executed via an Applescript executable File that has only one line of code:
do shell script "/Volumes/Work/createZipFile.sh"
The Bash Script opens Applescript which lets me prompt a folder (I know, kind of silly to open AS to run a Bash Script that runs AS). The variable is then used to zip this folders content without the .fla files.
myFolder=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfolder to choose folder with prompt "Select the Folder that you want to zip!"
end tell
return (posix path of myfolder)
EOT`
cd $myFolder
zip -r ZipMe.zip . -x ".fla"
echo "A zip File has been created"
So this script does actually work for some folder I try to zip.
But unfortunately not for every folder I chose. Sometimes (no idea why) it seems like it can not find the folder I chose with the prompt, so I starts (at least the zip-process starts running like crazy and doesn't stop) zipping my whole drive.
Any ideas what could be wrong?
In case anybody wants to use this script (which I highly doubt ;)), here is my final version of it.
#!/bin/bash
#Opens an applescript prompt window to select a folder
myFolder=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfolder to choose folder with prompt "Select the Folder that you want to Zip!"
end tell
return (posix path of myfolder)
EOT`
# Terminate if the path is empty (canceled)
if [ -z "$myFolder" ];
then
#echo "Chose a folder!"
exit 0
else
#Change the directory to the above selected folder
cd "$myFolder"
# Creates a ZipFile with todays date of the selected folder, neglecting the after -x listed filetypes
zip -r ZipFile_`eval date +%Y_%m_%d`.zip . -x "*.fla*" "*.AppleDouble*" "*.DS_Store*"
#echo "A zip File has been created"
fi
Your first step should be to figure out what "Kind" of file the .fla is. To do this, run this script and select one of your .fla files:
tell application "Finder"
set theFile to choose file
display dialog (kind of theFile) as string
end tell
And then to get all of the files BUT that type in any folder, you can run this script (Replacing "Plain Text" with whatever type your .fla's turn out to be):
tell application "Finder"
set thePath to choose folder
set theFiles to get every file of folder thePath whose kind is not equal to "Plain Text"
end tell
from there it's just a matter of zipping. After doing some quick googling it looks like the easiest way to zip from applescript is by using do shell script, which shouldn't be that bad now that you have all the files you need in a nifty little array. If you're going for speed though, I might suggest moving this whole project over to bash. That should also simplify things quite a bit. Best of luck!

Remove multiple PDF passwords with workflow or applescript in a folder

How do I remove passwords from multiple PDF files using Applescript or by creating a Workflow in OS X?
My scenario is that I have multiple password protected PDF files in a folder. I know the passwords for all, which is same. I want to be able to run a Workflow on this folder so that all PDFs inside it are unlocked by the workflow.
OR run an Applescript shell code on all these files at once
I also preferably want to be able to create a way where putting / moving / pasting any PDF in the folder automatically unlocks it :)
Help appreciated !!
Update:
I have tried pdftk. The following code works awesome in Terminal, once pdftk is installed
pdftk secured.pdf input_pw foopass output unsecured.pdf
Now I want to be able to create a workflow that runs this command on selected files or on all the files in a folder
The AppleScript command to execute a shell script is do shell script...
So something like this:
do shell script "pdftk secured.pdf input_pw foopass output unsecured.pdf"
should work.
At this point I see 2 options:
write an AppleScript script that ask the user for the folder or get it from the Finder selection and then execute the command for each file in the folder;
write an Automator workflow that get the files from the folder using already available actions and then attach a new action that execute the AppleScript script.
For option 2 you can set an Automator workflow as in the following image.
Have you heard of "Folder Actions"? It's a way to attach an applescript to a folder so that whenever a new file is added to the folder the applescript is run. A quick google search turned up this which will give you directions on how to set it up. You can do more google searching if you still have questions.
Here's an applescript you can use with folder actions. I didn't test it but it should work (it's basic code). This will do its stuff on only pdf files. Other files you add to the folder will be left alone. NOTE: you have to put in your values for the first 4 variables of the script.
Good luck.
on adding folder items to theFolder after receiving theItems
-- enter your values here
set pdftkPosixPath to "/usr/bin/pdftk"
set pWord to "foopass"
set appendedName to "_unlocked" -- text to append to the file name
set shouldTrash to true -- true or false, move the locked file to the trash after unlocking?
set fContainer to theFolder as text
repeat with anItem in theItems
try
tell application "System Events"
set fName to name of anItem
set fExt to name extension of anItem
end tell
if fExt is "pdf" and fName does not contain appendedName then
set baseName to (text 1 thru -5 of fName) & appendedName & ".pdf"
set newPath to fContainer & baseName
do shell script (quoted form of pdftkPosixPath & space & quoted form of POSIX path of anItem & " input_pw " & quoted form of pWord & " output " & quoted form of POSIX path of newPath)
if shouldTrash then
tell application "Finder" to move anItem to trash
end if
end if
end try
end repeat
end adding folder items to
EDIT: here's how you can ask for a password. Note that if you want to see the text then remove "with hidden answer".
display dialog "Enter a password:" default answer "" with icon note with hidden answer
set theAnswer to text returned of the result
if theAnswer is not "" then set pWord to theAnswer

AppleScript copy and paste file with right-click

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

Resources