Showing posts with label AppleScript. Show all posts
Showing posts with label AppleScript. Show all posts

Friday, January 2, 2015

AppleScript to Compress Files and Folders

I had the need to select several folders at once in the Mac OS Finder and zip them up as individual archives. This AppleScript to compress files and folders was the solution I came up with. The script compresses each item selected into its own archive, and works with both folders and files.


tell application "Finder"
set theItems to selection
repeat with i from 1 to (count of theItems)
set theItem to (item i of theItems) as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".zip")
do shell script "zip -jr " & zipFile & " " & itemPath
end repeat
end tell


As always, you'll get the best results when used with FastScripts by Red Sweater Software.

Friday, August 12, 2011

AppleScript to Add Empty Files in Finder Quickly

Today I had the need to add a bunch of named text files to a folder in the Finder on my Mac. I found it a major pain to open BBEdit, make a new document, save it to where I wanted it, and then manually copy & rename the file back in the Finder. A lot of effort just to get 7 or 8 empty files with different names.

So, I threw together this little AppleScript to add empty files instead. The gist is once launched in the Finder (using FastScripts by Red Sweater naturally. Keystroke: cmd+option+shift+N), a dialog pops up that lets you enter a file title. Then the Finder creates that file in the front-most window. If no window is open, the file is added to your Desktop.

I found this to be more than twice as fast as the "traditional" method of making several files. Hopefully, it can help you out too. Remember, use it with FastScripts for quick keystroke access. Save as a script in the "Finder" scripts folder ("~/Library/Scripts/Applications/Finder/").

Here's the code. Just copy and paste into AppleScript Editor and save...


property defaultFileName : "newFile.txt"
tell me to activate
set theFileName to text returned of (display dialog "Enter a file name:" default answer defaultFileName)
tell application "Finder"
activate
if the (count of windows) is not 0 then
set theFolder to (folder of the front window) as text
set theFolder to POSIX path of theFolder
else
set theFolder to POSIX path of (get path to desktop)
end if
set addedFile to (theFolder & theFileName)
do shell script "touch '" & addedFile & "'"
if the (count of windows) is not 0 then
set addedFile to (POSIX file addedFile) as alias
select addedFile
end if
end tell

Saturday, October 16, 2010

AppleScript to Toggle the Desktop

Here's an AppleScript I use to quickly toggle desktop visibility for taking screenshots and recording screencasts. Thought I'd share it with the world.



Copy and paste the code below into AppleScript Editor, or your editor of choice, compile and save.

As always, scripts like this work best using FastScripts from Red Sweater Software.


on run
tell application "System Events"
set frontMostApp to name of the first process whose frontmost is true
end tell
try
set theDefault to ((do shell script "defaults read com.apple.finder CreateDesktop") as integer) as boolean
on error -- if the default value doesn't already exist, create it...
do shell script "defaults write com.apple.finder CreateDesktop 1"
set theDefault to ((do shell script "defaults read com.apple.finder CreateDesktop") as integer) as boolean
end try
do shell script "defaults write com.apple.finder CreateDesktop " & (((not theDefault) as integer) as string)
tell application "Finder" to quit
delay 1
tell application "Finder" to launch
tell application frontMostApp to activate
end run


Note: This single script turns off the desktop if it's on, and turns it on if it's off - just to clear up the question should it need to be asked.