I have a program (Mullesight) that takes a snapshot and place a JPG inside my “snapshots” folder on my desktop. The problem is that it keeps saving the same same file name and won’t allow me to take more than one picture. I wish to have successive pictures named differently, such as picture1.jpg, picture2.jpg, etc., in my snapshots folder.
I copied a sample script from Mullesight’s author to take a snaphot and save a JPEG file. How can I modify it so that I can do what I want?
Thanks for any help.
try
tell application "MulleSight"
-- save prior state
set shouldScale to should scale
set useSelection to use selection
-- now set what we need
set should scale to false
set use selection to false
-- AppleScript still uses old file/alias magic by default
-- therefore we have to use POSIX
set srcImg to (POSIX path of "/tmp/picture.tiff")
-- save the image as TIFF (other formats are not supported)...
save in srcImg as "tiff"
-- reset prior state
set should scale to shouldScale
set use selection to useSelection
end tell
-- just for the fun of it, convert TIFF into JPEG
set dstImg to (POSIX path of "/tmp/picture.jpg")
tell application "Image Events"
launch
set img to open srcImg
save img as JPEG in file dstImg with icon
close img
end tell
on error errorMsg
display dialog errorMsg
end try
set dstImg to (POSIX path of "/tmp/picture.jpg")
tell application "Image Events"
launch
set img to open srcImg
save img as JPEG in file dstImg with icon
close img
end tell
Something like this:
tell application "Finder" to set t to name of (files of alias ((startup disk as text) & "private:tmp"))
set pctCount to 0
repeat with f in t
if contents of f contains "picture" then set pctCount to pctCount + 1
end repeat
set dstImg to (POSIX path of "/tmp/picture" & pctCount + 1 & ".jpg")
tell application "Image Events"
launch
set img to open srcImg
save img as JPEG in file dstImg with icon
close img
end tell
Thanks. I tried both responses but nothing after the first picture is saved. picture.jpg is saved in my Attachments fodler, but nothing subsequent upon repeated runs.
I tried another option suggested by Niel an the Apple Discussion group and incorporated it:
property dest : ((path to desktop folder as text) & “Attachments”)
try
tell application “MulleSight”
-- save prior state
set shouldScale to should scale
set useSelection to use selection
-- now set what we need
set should scale to false
set use selection to false
-- AppleScript still uses old file/alias magic by default
-- therefore we have to use POSIX
set srcImg to (POSIX path of "/tmp/picture.tiff")
-- save the image as TIFF (other formats are not supported)...
save in srcImg as "tiff"
-- reset prior state
set should scale to shouldScale
set use selection to useSelection
end tell
beep
tell application "Finder"
set my_string to "picture.jpg"
set my_number to 1
repeat
if exists item ("Attachments:" & my_string) then
set my_string to "picture" & my_number & ".jpg"
set my_number to my_number + 1
else
exit repeat
end if
end repeat
end tell
set dstImg to (POSIX path of "/tmp/" & my_string)
tell application "Image Events"
launch
set img to open srcImg
save img as JPEG in file dest
close img
end tell
Your script (no matter which version) is set up to run once. If you used my change to ImageEvents, it will name the pictures sequentially if there is already one present, but nothing in your script says “do it again”. As it is set up, you have to run it every time you want a picture.
I want it to sequentially take a picture and name it differently after each trigger event:
I press my Powermate button which activates a script that tells Mullesight to take a snapshot which saves it as a JPG (picture1.jpg) in a folder on my desktop. If I press again in 2 seconds, it saves another picture, but with a different name (picture2.jpg), and so on.
Ahh, but what tells this script to run? The script that takes the picture must then start this AppleScript (using osascript), or the “picture taker” must include this AppleScript as an osascript in the shell script. You didn’t show us that part. See man osascript for how to do that.
Mullescript is the applicaiton that takes the snapshot from whatever video imput source is availbale. (See http://www.mulle-kybernetik.com/software/MulleSight/). It’s the first part of the script. The author admits that the code is a little clunky, but it does work. I’m actaully using it now in a limited form. Once a picture.jpg file is placed in my Attachment’s folder, a folder action removes it and imports it to another application. So I wait a few seconds until it’s removed and I snap another picture. I wish to take more frequent pictures without having to wait.
I understood that part. You didn’t answer my question though: “what tells this script to run?” I can’t suggest how you might fix this if I don’t understand the entire process. You push a button. Then what?
Via Proxi, a tool from Griffen, pressing the Powermate button is programmed to trigger an Applescript from either within the program or to run a script file. The script then activates the video capture program (in this case Mullesight) and saves the file. Is this what you need?
Getting there. Assuming the script in this sentence: “Via Proxi, a tool from Griffen, pressing the Powermate button is programmed to trigger an Applescript from either within the program or to run a script file.” is the script we’re discussing, then the ‘tell application “MulleSight” … end tell’ block grabs the picture and saves it as /tmp/picture.tiff. The stuff after the beep gets you picture numbers and and uses Image Events to convert the tiff to jpeg. The original is left in /tmp/, called picture.tiff. If MulleSight won’t overwrite picture.tiff, it won’t save another picture. If you’re not saving those original tiffs, then you should delete each one after you convert it or move it somewhere else.
Note that I commented out the MulleScript portion (I didn’t install it, so can’t compile with it there).
property dest : ((path to desktop folder as text) & "Attachments")
try
(* tell application "MulleSight"
-- save prior state
set shouldScale to should scale
set useSelection to use selection
-- now set what we need
set should scale to false
set use selection to false
-- AppleScript still uses old file/alias magic by default
-- therefore we have to use POSIX
set srcImg to (POSIX path of "/tmp/picture.tiff")
-- save the image as TIFF (other formats are not supported)...
save in srcImg as "tiff"
-- reset prior state
set should scale to shouldScale
set use selection to useSelection
end tell
*)
beep
-- Using Jacques' numberer (it doesn't use the Finder)
set thesenames to list folder destFolder
set {nbr, n} to {2, "picture1.jpg"}
repeat while n is not in thesenames
set n to ("picture" & nbr & ".jpg")
set nbr to nbr + 1
end repeat
set dstImg to (destFolder & n)
set dstImg to (POSIX path of "/tmp/" & my_string)
tell application "Image Events"
launch
set img to open srcImg
save img as JPEG in file dest
close img
end tell
-- This deletes it - I don't mean trashes it - It's gone!
tell application "System Events" to delete alias ((path to desktop as text) & "picture.tiff")
on error errorMsg
display dialog errorMsg
end try
I noted that the number following the picture is set to incrementally go up by one after every snapshot, even if I quit and restart the script. It never resets to 1.
From a practical standpoint, it doesn’t matter what the file name of number is, but I can imagine the the numbers can get very large in a few years.
Is it possible to detect if there is a picture at all in the Attachments folder and if so, save the picture file with number +1? So if there are no pictures it automatically resets to one, and starts up again, as long as there are pictures in the folder.
Thanks. I’m learning a lot from this. I played around by setting some of the numerical variables to 0 at certain parts of the program, but no luck.
Change the first part of your (Jacques’) script to this:
property destFolder : (path to desktop folder as Unicode text) & "Attachments:"
property the_plist : quoted form of POSIX path of ((path to preferences folder as Unicode text) & "SnapshotCounter")
-- check Attachments folder - set plist to 0 if no picture files
tell application "System Events" to if (files of destFolder where its name contains "picture") is {} then do shell script "defaults write & the_plist & space & the_default & space & 0"
set nbr to (read_default("Startnumber", the_plist))
-- tell application "MulleSight"
--
tell application "System Events" to if (files of destFolder where its name contains "picture") is {} then do shell script "defaults write & the_plist & space & the_default & space & 0"
To
tell application "System Events" to if (files of alias destFolder where its name contains "picture") is {} then do shell script "defaults write & the_plist & space & the_default & space & 0"
I had used ‘choose folder’ to test, forgetting that that returned an alias.
Tried it. I got this (with no files in my attachements folder):
System Events got an error: sh: line 1: the_plist: command not found
sh: line 1: space: command not found
sh: line 1: the_default: command not found
sh: line 1: space: command not found
sh: line 1: 0: command not found
Try this instead - just delete the plist if there are no files in Attachments. It doesn’t check if there is an Attachments folder.
property destFolder : (path to desktop folder as Unicode text) & "Attachments:"
property the_plist : quoted form of POSIX path of ((path to preferences folder as Unicode text) & "SnapshotCounter")
-- check Attachments folder - set plist to 0 if no picture files
tell application "Finder" to if (files of alias destFolder where its name contains "picture") is {} then
try
delete ((path to preferences folder as string) & "SnapshotCounter.plist") as alias
end try
end if
set nbr to (read_default("Startnumber", the_plist))
tell application "MulleSight"
-- save prior state
set shouldScale to should scale
set useSelection to use selection
-- now set what we need
set should scale to false
set use selection to false
-- AppleScript still uses old file/alias magic by default
-- therefore we have to use POSIX
set srcImg to (POSIX path of "/tmp/picture.tiff")
-- save the image as TIFF (other formats are not supported)...
save in srcImg as "tiff"
-- reset prior state
set should scale to shouldScale
set use selection to useSelection
end tell
set dstImg to (destFolder & "picture" & nbr & ".jpg")
tell application "Image Events"
launch
tell (open srcImg)
save as JPEG in dstImg with icon
close
end tell
end tell
on read_default(the_default, the_plist)
try
set x to ((do shell script "defaults read " & the_plist & " " & the_default) as integer) + 1
do shell script "defaults write " & the_plist & " " & the_default & " " & x
return x
end try
do shell script "defaults write " & the_plist & " " & the_default & " 1"
return 1 -- if the Plist doesn't exist
end read_default
Thanks Adam for all your help. Unfortunately, the numbers just keep going up sequentially. I can use it as is, as the file name does not matter.
Would there be a way to reset the variable to 0 so that I can manually reset it once in a while, in case it gets in the thousands? What is the variable that sets the picture number?
Time for some detective work by you then sypark, because this works perfectly for me. In the script below, I’ve commented out all the MulleSight and Image Events stuff, and left just the numbering scheme so we can just test the numbering scheme by itself.
Do these checks (you’ve got to learn to do this yourself - it’s the essence of debugging a script):
In the Finder, under the “Go” menu, choose “Go to folder …” and in the window that pops up type ~/Library/Preferences/ and click “Go” or hit return. That Finder window that opens should contain a file “SnapshotCounter.plist”. Does it?
Again in the Finder, under Go>Go to folder… type ~/Desktop/Attachments/ and press Go or return. That should open a Finder window showing the “Attachments” folder. Does it?
Rename the Attachments folder to something else, i.e. Attachments* and create a new untitled folder named “Attachments”. Don’t put anything in it.
Now run the script below as is (don’t uncomment anything). The Result pane of your Script Editor should read 1. If not, tell me what it says.
Create a file or rename a copy of any file at all to “picture” – it doesn’t matter what the extension is. Put it in “Attachments”. Run the script again. The results pane should say 2. Run it again, get 3, incrementing for each run.
Now remove the file or rename it to something that doesn’t contain “picture” (doesn’t matter what it is). Run the script again, and the result pane should say 1. If not, tell me.
Remove the file “SnapshotCounter.plist” from your preferences folder (or delete it). Run the script again. Repeat test 6. Now what.
property destFolder : (path to desktop folder as Unicode text) & "Attachments:"
property the_plist : quoted form of POSIX path of ((path to preferences folder as Unicode text) & "SnapshotCounter")
-- check Attachments folder - set plist to 0 if no picture files
tell application "Finder" to if (files of alias destFolder where its name contains "picture") is {} then
try
delete ((path to preferences folder as string) & "SnapshotCounter.plist") as alias
on error e
display dialog e
end try
end if
set nbr to (read_default("Startnumber", the_plist))
(*
tell application "MulleSight"
-- save prior state
set shouldScale to should scale
set useSelection to use selection
-- now set what we need
set should scale to false
set use selection to false
-- AppleScript still uses old file/alias magic by default
-- therefore we have to use POSIX
set srcImg to (POSIX path of "/tmp/picture.tiff")
-- save the image as TIFF (other formats are not supported)...
save in srcImg as "tiff"
-- reset prior state
set should scale to shouldScale
set use selection to useSelection
end tell
set dstImg to (destFolder & "picture" & nbr & ".jpg")
tell application "Image Events"
launch
tell (open srcImg)
save as JPEG in dstImg with icon
close
end tell
end tell
*)
on read_default(the_default, the_plist)
try
set x to ((do shell script "defaults read " & the_plist & " " & the_default) as integer) + 1
do shell script "defaults write " & the_plist & " " & the_default & " " & x
return x
end try
do shell script "defaults write " & the_plist & " " & the_default & " 1"
return 1 -- if the Plist doesn't exist
end read_default