Change directory for screenshot from applescript - applescript

I am making a script that takes a picture of whoever opens it by taking a screenshot of PhotoBooth. However, I do not know how to change the directory of the screenshoot, because I just started AppleScript. For example, I want it to go the folder
/Users/ADMIN/Desktop/AllFolders/Folder1/Folder2
How would I do that?
Heres my code:
tell application "Photo Booth"
activate
set volume output volume 0
tell application "Finder" to set visible of process "Photo
Booth" to false
end tell
property N : 0
set N to N + 1
delay 0.1
set picPath to ((POSIX path of (path to desktop)) & "Picture_" & N & ".png") as string
do shell script "screencapture -tjpg " & quoted form of
picPath
tell application "Photo Booth"
quit
end tell

I finally figured it out without using iSightCapture!
First instead of making photo booth invisible, I close it immediately after taking a screenshot. Thanks #CJK for that bugfix.
To answer the problem, I found that the code:
set picPath to ((POSIX path of (path to desktop)) & "Picture_" & N & ".png") as string
do shell script "screencapture -tjpg " & quoted form of picPath
was the answer to this problem.
I got rid of the N addition to simplify it.
Instead of
POSIX path of (path to desktop)
I changed the path.
set picPath to (POSIX path of "/Users/ADMIN/Desktop/folder1/folder2/nameoffile ") & ".png" as string
This worked :)

Download isightcapture utility for Mac (Google search, thanks to axel#intergalatic.de to made this a freeware !), and store the file somewhere on your Mac, for instance in your Documents folder.
Use this shell command in Applescript via 'do shell script' instruction :
do shell script "~/Documents/isightcapture ~/Desktop/Photo.jpg"
The '~/Documents' is the path to your isightcapture command : here in your Documents folder.
The '~/Desktop/Photo.jpg' stores the picture made on Desktop with name Photo.jpg. Of course it could be any of your other folders !
There are several parameters for that command (isightcapture -h will give yo more options)
-t option to specify the photo format : jpg (by default) , png, tiff, bmp
-w and -h to define picture size
Do shell script ~/Documents/isightcapture -w 320 -h 240 -t png ~/Documents/Folder1/P1.png
This script take a photo and store it a png format in file P1.png in folder Folder1 in my Documents folder. The picture size is 320 x 240.
Note : this utility works since Snowleopard (OS 10.6) and I tested it up to OS 10.12. I don't see any reason why it would no work on next versions.

Related

Workflow stopped working after changing to MacBook Pro M1

I have a problem with a workflow that stopped working when I upgraded to a MacBook Pro M1.
This workflow was used as a Quick Action in Finder files. First I tried running directly from Automator, and to my surprise it works, then tried again as Quick Action and doesn't work. I have been searching the Internet for more than a month and trying with different approaches but can't make it work again from Quick Action.
I have extracted just the code with the problem for easier explanation. The intention is to get the size of a movie file. The following code works inside Automator but issues an error when run from a Quick Action: cannot find the file.
tell application "Finder"
set existingMovies to (every file of theFolder) as alias list
end tell
repeat with movie in existingMovies
--repeat with movie in input
set theMovie to the (quoted form of POSIX path of movie) as string
set movieSize to do shell script "/usr/bin/mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & theMovie
end repeat
Then I tried working with input parameter of the workflow (in this case a list of files since this workflow uses "Workflow receives current: movie files"). With this approach it works both from Automator and as Finder Action
repeat with movie in input
set theMovie to the (quoted form of POSIX path of movie) as string
set movieSize to do shell script "/usr/bin/mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & theMovie
end repeat
In all scenarios I am using the same folder.
It seems that the list of files specified by input is somehow different from the list of files I create from a specified folder (existingMovies).
Any idea why this happens?
I can't replicate your problem on my machine, so I can't say for sure what's going wrong. But here's a 'best practices' reworking of your script. See if it solves your problem.
on run {input, parameters}
-- always use System Events, not the Finder, where possible
tell application "System Events"
repeat with thisFolder in input
set existingMovies to (every file of thisFolder)
repeat with movie in existingMovies
-- dereference properties with 'get'
set theMovie to the (quoted form of (get POSIX path of movie))
set movieSize to do shell script "/usr/bin/mdls -name kMDItemDurationSeconds -raw -nullMarker 0 " & theMovie
end repeat
end repeat
end tell
return input
end run

File associations in OS X: doesn't pass filename in args?

I am packaging an application into a .app directory for "drag install" or whatever it's called and I have a weird iessue with file association.
I set my application as a viewer for .xyz files, and the system does start my app when I double click that file; the only problem is that the path of the file I clicked is nowhere in the args[], there's only one parameter that is something like ~psn_0_901340 and I think is a timestamp because it changes every time.
So... what am I supposed to do? I've been sitting here for 2 hours straight and can't find a solution.
I think what you want is an AppleScript droplet.
A shortened version of the AppleScript from that link:
on open dropped_files
set the_command to quoted form of POSIX path of (path to resource "script.sh")
set file_list to ""
repeat with file_path in dropped_files
set file_list to file_list & " " & quoted form of POSIX path of file_path
end repeat
set the_command to the_command & file_list
do shell script the_command
end open
Export as an application using Script Editor. Place script.sh in the Resources folder.
Add your file extension associations to Info.plist. You may need to launch or move the droplet before OS X notices the change & allows you to double-click files.
If you want to launch Terminal or capture the script output, see the full AppleScript.

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

How can I download a file using AppleScript?

Something like:
set the file_tgt to (path to downloads as string) as file specification
set file_src to "http://my_file.png"
tell application "URL Access Scripting"
download file_src to file_tgt
end tell
How do I get the path to the default downloads folder for the user?
AppleScript doesn't like the path conversion. How can I fix that?
I found the solution myself:
Use "path to downloads folder"
set the file_tgt to (path to downloads folder as string) & "file.png"
To download a collection of files hosted in a directory using applescript and shell script -
set counter to 0
repeat 36 times
do shell script "curl -f 'http://stage.omatics.com/Images/Devices/Devices/" & counter & ".png' -o ~/Desktop/" & counter & ".png"
set counter to counter + 10
delay 2
end repeat
This will download these 36 files
http://stage.omatics.com/Images/Devices/Devices/0.png
http://stage.omatics.com/Images/Devices/Devices/10.png
http://stage.omatics.com/Images/Devices/Devices/20.png
--
--
http://stage.omatics.com/Images/Devices/Devices/350.png
at your desktop.
Hope this helps.

Delete file without playing sound in Applescript?

tell application "Finder"
set deletedfile to alias "Snow Leopard:Users:test.pdf"
delete deletedfile
end tell
The problem is I repeatedly call this script from my Cocoa application so the sound is played repeatedly too. Is it possible to disable that sound ?
Since the trash is just an invisible folder inside your home folder you can do this...
set myFile to (path to desktop folder as text) & "myFile.txt"
set trashFolder to path to trash folder from user domain
do shell script "mv " & quoted form of POSIX path of myFile & space & quoted form of POSIX path of trashFolder
one simple way (doesn't move to Trash)
do shell script "rm '/Users/test.pdf'"

Resources