am trying to capture screenshot and dump into folder on the desktop with the applescript..
i was successful in taking the screenshot but not dumping into folder which is not existing on the desktop.. Pls suggest..
I tried..
set loc to "/Users/username/Desktop/New Folder"
property N : 0
set N to N + 1
set picPath to (loc & "Picture_" & N & ".png") as string
do shell script "screencapture -tjpg " & quoted form of picPath
After the first line (set loc...) add the following line:
do shell script "mkdir -p " & quoted form of loc
Related
I'm trying to write a small gpx file conversion script. The conversion part is done but I can't fix the naming of the converted file. I want the filename of the converted file like this:
originalfilename.2.gpx
This is the code I have so far:
-- begin script
-- For converting from one format to another. Preset to convert from GPX to KML. Tailor as needed. Read the documentation for your version
--Based on the script by Robert Nixon, http://www.gpsbabel.org/os/OSX_oneclick.html
-- This is set up for gpsbabel installed using DarwinPorts. See <http://www.gpsbabel.org/osnotes.html> for details
-- This script is not being actively supported, but you may get some help from the gpsbabel discussion group: <http://www.gpsbabel.org/lists.html>
--start script
-- HINT a '~/' is your home folder and of course the double hyphen is a comment. Remove to make active.
-- setting path for gpsbabel. Reset as needed
--set gpsBabelLocation to "/opt/local/var/db/dports/software/gpsbabel/1.2.7_0/opt/local/bin/" -- DarwinPorts Default for Tiger, Feb. 2006
set gpsBabelLocation to "/Applications/GPSBabelFE.app/Contents/MacOS/" -- Normal MacGPSBabel location
--set gpsBabelLocation to ""
set inputTYPE to "gpx" -- set the input file type to whatever you want
set outputTYPE to "gpx" --set the output file type to whatever you want
set strPath to POSIX file "/Users/someusername/downloads/" --set the default path for file input dialog
--set inputPATH to "~/Desktop/route.gpx" --set the path to where your input file is
-- comment out the following line if you want to use the line above
set inputPATHalias to (choose file with prompt "Select a GPX file to convert to " & outputTYPE & ":" default location strPath)
-- set inputPATH to quoted form of POSIX path of inputPATHalias -- got error, but two step below works.
set inputfilename to (inputPATHalias as text)
set inputPATH to POSIX path of inputPATHalias
set inputPATH to quoted form of inputPATH -- needed to pass to shell script
--set outputPATH to "~/Desktop/yournewfile.wpt" --set the path to where your output file will be
set outputFileName to "converted." & inputTYPE & ".2." & outputTYPE
--set outputFileName to outputFileName & ".2." & outputTYPE
set outputFolderalias to choose folder with prompt "Select a folder for converted file:"
set outputPATH to fileCheckName(outputFolderalias, outputFileName)
-- See the Documentation for normal usage. http://www.gpsbabel.org/readme.html#d0e1388
-- Using the Position filter to correct a Garmin error. When the Garmin loses sight of the sattiletes, when it reconects, it puts in poitns with a lat-long equal to the last good point, but current time and elevation (mine has a barometer). Garmin: remove extraneous points recorded when out of range that have same lat and long as last good point (altitude is ignored in this filter).
set filterSetting to "" -- use this one if you don't want any Position filtering. You can leave in uncommented and any below will override it.
-- set filterSetting to " -x position " -- Default, distance=0
-- set filterSetting to " -x position,distance=1f " -- 1 foot and leave one
-- set filterSetting to " -x position,distance=1m " -- 1 meter and leave one
-- set filterSetting to " -x position,all" -- remove all duplicates
-- set filterSetting to " -x simplify,count=1000"
-- set filterSetting to " -x simplify,error=1k" -- doesnt' work, but then it's not in the manual
-- set filterSetting to ""
tell application "System Events"
activate
do shell script gpsBabelLocation & "gpsbabel " & filterSetting & "-i " & inputTYPE & " -f " & inputPATH & " -x transform,trk=rte " & " -o " & outputTYPE & " -F " & outputPATH
end tell
--Handlers. Only used once, but I've used them elsewhere
on fileCheckName(outputFolderalias, outputFileName)
set outputFolder to POSIX path of outputFolderalias
set outputPATHnoQuote to outputFolder & "/" & outputFileName -- may need to change this if the extension isn't the same as the outputType, The Desktop is a common location
set outputPATH to quoted form of outputPATHnoQuote -- needed to pass to shell script
--check if file already exist and if we want to overwrite it
set outputFolderaliasText to outputFolderalias as text
set outputPATHcolon to outputFolderaliasText & outputFileName
tell application "Finder" -- trying to get handler to work
if file outputPATHcolon exists then display dialog "The file " & outputFileName & " already exists. Do you want to overwrite it?"
end tell
return outputPATH
end fileCheckName
--end script
I'm unable to find Applescript code that translates the filename from a prompt into a variable.
I recommend to not use the Finder.app at all for this task. Especially, checking file existence with the Posix file coercion inside the tell block is a big mistake. The Finder is also not needed to get the file basename. For this task, with the help of other experienced users, I developed a high-speed handler, provided here.
In general, I recommend using the Finder only when absolutely necessary, because it is often busy in the background with other time-consuming tasks.
set gpsBabelLocation to "/Applications/GPSBabelFE.app/Contents/MacOS/" -- Normal MacGPSBabel location
set inputType to "gpx" -- the input file type extension
set outputType to "gpx" -- the output file type extension
set defaultPath to (path to downloads folder)
set inputPath to (choose file of type inputType with prompt "Select a " & inputType & " file to convert to " & outputType & ":" default location defaultPath) -- remove 'of type inputType' to select any file
set outputFileName to getBaseName(POSIX path of inputPath) & ".2." & outputType -- the converted name
set outputFolder to choose folder with prompt "Select a folder for the converted file:"
set outputPath to fileCheckName(outputFolder, outputFileName)
set filterSetting to ""
set inputPath to quoted form of (POSIX path of inputPath)
set command to gpsBabelLocation & "gpsbabel " & filterSetting & "-i " & inputType & " -f " & inputPath & " -x transform,trk=rte " & " -o " & outputType & " -F " & outputPath
log command
if gpsBabelLocation is not "" then
do shell script command
log result
end if
on getBaseName(posixPath)
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
if (posixPath ends with "/") then
set baseName to text item -2 of posixPath
else
set baseName to text item -1 of posixPath
end if
if (baseName contains ".") then
set AppleScript's text item delimiters to "."
set baseName to text 1 thru text item -2 of baseName
end if
set AppleScript's text item delimiters to ATID
return baseName
end getBaseName
on fileCheckName(outputFolder, outputFileName) -- check if file exists
try
alias (outputFolder & outputFileName)
display alert quoted form of outputFileName & " already exists. Do you want to replace it?" message "A file or folder with the same name already exists at " & quoted form of POSIX path of outputFolder & ". Replacing it will overwrite its current contents." buttons {"OK", "Cancel"}
if button returned of the result is "Cancel" then error number -128 -- alert doesn't automatically cancel
end try
return quoted form of ((POSIX path of outputFolder) & outputFileName) -- quote for the shell script
end fileCheckName
Finder or System Events can be used to get file names, you just need to be a little careful when using the Finder, since it doesn't know that much about POSIX paths. With the various quoting for the shell, you also need to manipulate the various name pieces before quoting. Shortened and cleaned up a bit, your script would be something like:
--Based on the script by Robert Nixon, http://www.gpsbabel.org/os/OSX_oneclick.html
-- set path for gpsbabel as needed:
-- set gpsBabelLocation to "/opt/local/var/db/dports/software/gpsbabel/1.2.7_0/opt/local/bin/" -- DarwinPorts Default for Tiger, Feb. 2006
set gpsBabelLocation to "/Applications/GPSBabelFE.app/Contents/MacOS/" -- Normal MacGPSBabel location
-- set gpsBabelLocation to "" -- testing, etc
set inputType to "gpx" -- the input file type extension
set outputType to "gpx" -- the output file type extension
set defaultPath to (path to downloads folder)
set inputPath to (choose file of type inputType with prompt "Select a " & inputType & " file to convert to " & outputType & ":" default location defaultPath) -- remove 'of type inputType' to select any file
tell application "Finder" to set {inputName, inputExt} to {name, name extension} of inputPath
if inputExt is not "" then set inputName to text 1 thru -((count inputExt) + 2) of inputName -- just the name part
set inputPath to quoted form of POSIX path of inputPath -- quote for the shell script
set outputFileName to inputName & ".2." & outputType -- the converted name
set outputFolder to choose folder with prompt "Select a folder for the converted file:"
set outputPath to fileCheckName(outputFolder, outputFileName)
-- See the Documentation for normal usage. http://www.gpsbabel.org/readme.html#d0e1388
set filterSetting to "" -- uncomment any below to override no position filtering
-- set filterSetting to " -x position " -- Default, distance=0
-- set filterSetting to " -x position,distance=1f " -- 1 foot and leave one
-- set filterSetting to " -x position,distance=1m " -- 1 meter and leave one
-- set filterSetting to " -x position,all" -- remove all duplicates
-- set filterSetting to " -x simplify,count=1000"
set command to gpsBabelLocation & "gpsbabel " & filterSetting & "-i " & inputType & " -f " & inputPath & " -x transform,trk=rte " & " -o " & outputType & " -F " & outputPath
log command
if gpsBabelLocation is not "" then
do shell script command
log result
end if
on fileCheckName(outputFolder, outputFileName) -- check if file exists
set outputPath to (POSIX path of outputFolder) & outputFileName
tell application "Finder" to if (outputPath as POSIX file) exists then -- match the save alert
display alert quoted form of outputFileName & " already exists. Do you want to replace it?" message "A file or folder with the same name already exists at " & quoted form of POSIX path of outputFolder & ". Replacing it will overwrite its current contents." buttons {"OK", "Cancel"}
if button returned of the result is "Cancel" then error number -128 -- alert doesn't automatically cancel
end if
return quoted form of outputPath -- quote for the shell script
end fileCheckName
With the script we are going to create another script where will be store position of all the elements of the desktop, the created script will be compile and usable to put back in place all the elements previously protected.
/adesktopsave/deskico.txt it is the temporary file which will be of use to the compilation.
/adesktopsave/savedicoposition.scpt It is the script of saving that is compiled to be used with applescrit
All the names used here exist that just for the example. These names have no particular property.
It is just necessary to plan to create a folder before using this script. Here it is:
/adesktopsave
Something else, end of line (\n) after " try
"
also " end try
"
and & "}
")
Are very important to respect so that the text is usable.
tell application "Finder" to set theList to {name, desktop position} of items of desktop
try
do shell script "rm -f /adesktopsave/deskico.txt"
do shell script "echo tell application " & quoted form of (quote & "Finder" & quote) & return & " >>/adesktopsave/deskico.txt"
end try
set n to (count (first item of theList))
repeat with i from 1 to n
set inp to do shell script "echo " & quoted form of (item i of first item of theList)
set xy to (item i of second item of theList)
set AppleScript's text item delimiters to ","
set xyz to do shell script "echo " & xy
set wxyz to ("{" & xyz & "}
")
set ligne to "try
" & "set desktop position of item " & quoted form of (quote & inp & quote) & " of desktop to " & quoted form of (wxyz) & "end try
"
set ligne to do shell script "echo " & ligne & " >>/adesktopsave/deskico.txt"
end repeat
do shell script "echo " & "end tell" & return & " >>/adesktopsave/deskico.txt"
display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save the positions of icons"
if (button returned of result) is "Cancel" then
set n to do shell script "echo " & n
else
do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt" & " /adesktopsave/deskico.txt"
end if
return n
We can lighten the script to its simplest expression. At the risk of having errors can be.
set ligne to ""
do shell script "mkdir -p /adesktopsave"
tell application "Finder" to set {names, positions} to {name, desktop position} of items of the desktop
set ligne to "tell application \"Finder\"
"
set n to (count names)
set AppleScript's text item delimiters to ","
repeat with i from 1 to n
set ligne to ligne & ("try
" & "set desktop position of item " & (quote & item i of names & quote) & " to {" & item i of positions & "}
end try
")
end repeat
set ligne to ligne & ("end tell" & return)
display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save the positions of icons"
if (button returned of result) is "Cancel" then
set n to do shell script "echo " & n
else
do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt -e " & quoted form of ligne
end if
set AppleScript's text item delimiters to ""
tell application "Finder" to open POSIX file "/adesktopsave/savedicoposition.scpt"
return n
I'm writing an AppleScript that will save the number of times it has ever ran to a .txt file on my desktop. It is working great so far, except it won't overwrite the old data. Here is my code:
property numberOfUnitTests : 0
set numberOfUnitTests to numberOfUnitTests + 1
display dialog "Number of unit tests ran to date: " & numberOfUnitTests & ""
set the logFile to ((path to desktop) as text) & "log.txt"
set the logText to "Number of unit tests ran to date: " & numberOfUnitTests & ""
try
open for access file the logFile with write permission
write ((logText) & return) to file the logFile starting at eof
close access file the logFile
on error
try
close access file the logFile
end try
end try
Now, this never over writes it. It just adds to the end of the file on a new line. I've tried adding in set eof logFile to 0, but then just nothing will save.
You are appending to the file:
starting at eof
by starting at the End Of File, not at the beginning...
property numberOfTimesRan : 0
set numberOfTimesRan to numberOfTimesRan + 1
display dialog "Number of tests ran to date: " & numberOfTimesRan & ""
set the logFile to ((path to desktop) as text) & "test.txt"
set the logText to numberOfTimesRan
try
open for access file the logFile with write permission
write ((logText as string) & return) to file the logFile --- starting at eof
close access file the logFile
on error
try
close access file the logFile
end try
end try
Also you were need to coerce the data to be a string
I would do it like this...
property numberOfTimesRan : 0
set numberOfTimesRan to numberOfTimesRan + 1
display dialog "Number of tests ran to date: " & numberOfTimesRan
set the logFile to POSIX path of (path to desktop as text) & "test.txt"
do shell script "echo " & numberOfTimesRan & " > " & quoted form of logFile
In brief, I need to do something like this:
I have a folder with a lot of files and want to process all files with extension .epub.
All files already follow a naming scheme: Lastname, Firstname - Title.epub or Lastname, Firstname - Series x - Title.epub and I need a parser for Lastname, Firstname, Series (if existing) and Title.
I have a command-line tool that sets metadata: ebook-meta filename -a "Firstname Lastname" -t Title
There are many snipplets for 1.), however I am in need for input for 2.) and appreciate any help/pointers!
You can start with the following and change it to meet your needs. It compiles, although untested.
set p to POSIX file "/Users/kaass/Desktop/test/"
tell application "Finder" to set filelist to name of every file of folder p
repeat with filename in filelist
set text item delimiters to ""
if text -5 thru -1 of filename is equal to ".epub" then
set temp to items 1 thru -6 of filename as text
set text item delimiters to " - "
set myWord to text items 1 thru -1 of temp
set title to myWord's last item as text
if myWord's length is equal to 3 then set series to myWord's second item as text
set myWord to item 1 of myWord as text
if myWord contains "," then
set text item delimiters to ", "
else
set text item delimiters to " "
end if
set author to (text item 2 of myWord) & space & (text item 1 of myWord)
set path_and_filename to POSIX path of file p & filename
do shell script "echo Processing file " & quoted form of path_and_filename & ": " & author & " +++ " & title
do shell script "/Applications/calibre.app/Contents/MacOS/ebook-meta " & quoted form of path_and_filename & " -a " & quoted form of author & " -t " & quoted form of title
end if
end repeat
Just comment if you need something to be changed.
In the past I had created an applescript for datedspace but which is not running anymore.
I would like to create an applescript that would look inside a folder ~/Documents/pool/ or ~/Downloads.
Let's say we are on February 7, 2011.
Check if the ~/Documents/2011/02/07/ exists
If not create ~/Documents/2011/02/07/
Move the files and/or folders inside ~/Documents/pool/ to ~/Documents/2011/02/07/
Bonus
Create a text file in ~/Documents/2011/ with the name index.mdown and containing the following format
# Index 2011
## January
[…]
## February
* 2011-02-07: Foo.jpeg
* 2011-02-07: Bar/
Lead or incomplete solutions are welcome.
Previous script
-- Script to automaticaly save documents in a date space.
-- Karl Dubost - http://www.la-grange.net/ - <karl#*******> - 2001 ©
-- Feel free to distribute it and modify it
-- Feel free to send me improvements
-- ********************************************************
-- Version 1.1 2001-03-30
-- Add control on existence of folders year and month
-- Make it more general based on Startup Disk
-- Version 1.0 2001-03-29
-- Creation of the code
-- Thanks to Bill Briggs
-- http://maccentral.macworld.com/columns/briggs.shtml
-- ********************************************************
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set yourDisk to name of startup disk as string
end tell
set todaysDate to (current date)
set {d, m, y} to {day, month, year} of todaysDate
set monthList to {January, February, March, April, May, June, ¬
July, August, September, October, November, December}
repeat with i from 1 to 12
if m = (item i of monthList) then
set monthString to text -2 thru -1 of ("0" & i)
exit repeat
end if
end repeat
set y to y as string
set dayString to text -2 thru -1 of ("0" & d)
set dayString to dayString as string
set datedFolder to yourDisk & ":Documents:" & y & ":" & monthString & ":" & dayString & ":" as string
set monthFolder to yourDisk & ":Documents:" & y & ":" & monthString & ":" as string
set yearFolder to yourDisk & ":Documents:" & y & ":" as string
set rootFolder to yourDisk & ":Documents:" as string
tell application "Finder"
if (folder datedFolder exists) then
repeat with oneFile in added_items
move oneFile to folder datedFolder
end repeat
else if not (folder yearFolder exists) then
make new folder at folder rootFolder with properties {name:y}
else if not (folder monthFolder exists) then
make new folder at folder yearFolder with properties {name:monthString}
else
make new folder at folder monthFolder with properties {name:dayString}
repeat with oneFile in added_items
move oneFile to folder datedFolder
end repeat
end if
end tell
end adding folder items to
I hope this will be good enough :-) :
With the help of some basic command line applications it creates subfolders in your ~/Documents folder (mkdir -p: it will not overwrite existing directories!), moves files there (mv -n: not overwriting files with the same name), then creates monthly logfiles containing the names of addedItems and finally a index.mdown file with all monthly logfiles which should look like this:
2011/01/03: testfile9
2011/02/01: testfile10
2011/02/07: testfile11
Now go ahead and attach it to your folder, give it a try! (maybe uncomment baseFolder and change it to /tmp/test or something, or to POSIX path of thisFolder, if you want it to clean up the folder it´s attached to.)
property dateFormatForFolder : "+%Y/%m/%d"
property dateFormatForMonthlyLog : "+%Y/%m/"
property dateFormatForYearlyLog : "+%Y/"
on adding folder items to thisFolder after receiving addedItems
set baseFolder to POSIX path of (path to documents folder)
set todaysFolder to do shell script "/bin/date '" & dateFormatForFolder & "'"
set monthsFolder to do shell script "/bin/date '" & dateFormatForMonthlyLog & "'"
set yearsFolder to do shell script "/bin/date '" & dateFormatForYearlyLog & "'"
set fullPath to baseFolder & todaysFolder
createFolder(fullPath)
repeat with i from 1 to number of items in addedItems
set oneItemsPath to (quoted form of POSIX path of item i of addedItems)
try
moveToFolder(fullPath, oneItemsPath)
end try
set fileName to name of (info for item i of addedItems)
set logThis to todaysFolder & ": " & fileName
do shell script "echo " & logThis & " >> " & baseFolder & monthsFolder & "log.txt"
end repeat
do shell script "find " & baseFolder & yearsFolder & " -type f -name 'log.txt' -print0 | xargs -0 cat > " & baseFolder & yearsFolder & "index.mdown"
end adding folder items to
on createFolder(fullPath)
do shell script "/bin/mkdir -p " & quoted form of fullPath
end createFolder
on moveToFolder(fullPath, oneItemsPath)
do shell script "/bin/mv -n " & oneItemsPath & " " & fullPath
end moveToFolder