I noticed that the command line 'zip' tool and Mac OS X's 'Compress XXX' option (available via right click in finder) are giving different output files. Not only is the size of the file a few hundred bytes bigger but the content is significantly different as well.
How can I find out what command the Finder is using for compression?
The answer is in man ditto:
The command:
ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip
will create a PKZip archive similarly to the Finder's Compress function-
ality.
Take a look at An AppleScript to compress a Finder selection article.
try
tell application "Finder"
set theSelection to the selection
set selectionCount to count of theSelection
if selectionCount is greater than 1 then
error "Please select only one Finder item before running this script."
else if selectionCount is less than 1 then
error "Please select one Finder item before running this script."
else
set theItem to (item 1 of theSelection) as alias
set destFolder to (container of theItem) as alias
set itemName to name of theItem
end if
end tell
do shell script ("ditto -c -k --sequesterRsrc --keepParent " & quoted form of POSIX path of theItem & space & quoted form of (POSIX path of destFolder & itemName & ".zip"))
on error theError
tell me
activate
display dialog "Error: " & theError buttons {"OK"} default button 1 with icon stop
end tell
end try
Related
I am completely newbie in Applescript...
In Apple Mail, you can save an email as a text file : File > Save As... > Format "Plain Text"
Is it possible to automate that using an applescript ?
Would it be possible to use applescript to save the mail (in txt "format") in the clipboard rather that as a txt file ?
Many thanks.
The Mail.app doesn't export selected message to TXT programatically. You can use GUI scripting, but GUI scripting is bad solution in this case. Exists other way, described here.
The script assumes the message is selected in the Mail.app and creates text file on the desktop, but you can indicate other folder.
.
-- Mail.app: Export Selected Message to plain text (.txt) file
-- written: by me, right now
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
set timeZone to (abbreviation of (current application's NSTimeZone's localTimeZone())) as text
-- Mail.app part (load remote content, get message properties)
tell application "Mail"
set download html attachments to true -- load remote content as well
tell (item 1 of (get selection)) -- here we tell to 1st selected message
set {theSubject, fromHider} to {subject, "From: " & sender}
tell (get date sent) -- build here "Date: ....." text line
set dateSentHider to "Date: " & date string & " - " & time string & " " & timeZone
end tell
set {toHider, messageContent} to {"To: " & address of to recipient 1, content}
end tell
end tell
set allText to fromHider & linefeed & "Subject: " & theSubject & linefeed & dateSentHider & ¬
linefeed & toHider & linefeed & linefeed & messageContent
-- replace every ":" symbol in theSubject with "_"
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set {itemList, AppleScript's text item delimiters} to {text items of theSubject, "_"}
set {theSubject, AppleScript's text item delimiters} to {itemList as text, ATID}
-- build destination file's Posix path, write to it
set theFile to POSIX path of (path to desktop folder) & theSubject & ".txt"
-- or,
-- set theFile to POSIX path of (choose folder) & theSubject & ".txt"
(current application's NSString's stringWithString:allText)'s writeToFile:theFile atomically:true ¬
encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
You can save message (in txt "format") in the clipboard as well. Simply:
set the clipboard to allText
I have following script :)
property word_docs : {"org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc"}
property default_path : (path to desktop) as alias
property Delim : {".docx", ".doc"}
property PDF : ".pdf"
set outPDF to {}
set selected_files to (choose file of type word_docs default location default_path with multiple selections allowed without invisibles and showing package contents)
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, Delim}
repeat with afile in selected_files
copy item 1 of text items of (afile as text) & PDF to the end of outPDF
end repeat
set AppleScript's text item delimiters to TID
tell application id "com.microsoft.Word"
activate
repeat with i from 1 to count of selected_files
open (item i of selected_files)
set theOutputPath to (item 1 of outPDF)
-- close access (open for access exportDocument)
tell active document
save as it file name theOutputPath file format format PDF
close saving no
end tell
end repeat
end tell
return
That helps me convert doc & docx files -> pdf, but it is too interactive.
And I have idea to run this script via terminal and i want to pass file path or directory as argument
for example:
$ script /Users/test/dest_dir/ /Users/test/out_dir/
will produce all pdf files into out_dir.
I saw this library also but it converts only docx files:
https://pypi.org/project/docx2pdf/
Is there anyone here who can help me rewrite this script .. I don't understand this language at all. or maybe someone will point to the finished tool. I need to do this on the mac os operating system.
see http://hints.macworld.com/article.php?story=20050523140439734
argv is an array of arguments in string format
on run argv
return item 1 of argv
This is the error that pops up when I try my script out:
Script Error ---- Can’t make {button returned:"Enter", text returned:"testing"} into type Unicode text.
I'm trying to have users enter a Username so that they that have their Documents folder be linked to a file server.
I have that part separate but now on this part of having the user input their username so when I have ln -s /Volumes/Drive/Documents Documents the username will automatically pull from the script and input it by itself so the Documents folder and File server could be linked.
Basically trying to make it to where the user doesn't have to go into the terminal and link the two together. I'm not sure why the error code is popping up other than it is probably having to deal with the set Username part of the script so not completely sure.
set Username to (display dialog "Enter your NetID Username" default answer "" buttons {"Cancel", "Enter"} default button 2)
tell application "Terminal"
activate
do script "'ln -s /Volumes/Drive/Documents Documents'" & Username
end tell
return input
The error message
Can’t make {button returned:"Enter", text returned:"testing"} into type Unicode text.
is pretty clear.
display dialog returns a record, not a string so you have to write
set Username to text returned of (display dialog "Enter your NetID Username" default answer "" buttons {"Cancel", "Enter"} default button 2)
Or if you need to handle the button and the returned text write
set {button returned:buttonReturned, text returned:textReturned} to display dialog "Enter your NetID Username" default answer "" buttons {"Cancel", "Enter"} default button 2
I have a script to copy all email attachments in a selected set of emails to a folder after some renaming. Because the attachments sometimes have identical names at origin, even with renaming, I need to add something like " copy" to subsequent versions so they don't save atop one another. With some programming knowledge but very little understanding of AppleScript, I cobbled this together:
tell application "Mail"
set theMessages to selection
set theOutputFolder to (choose folder) as string
repeat with a from 1 to length of theMessages
set theMessage to item a of theMessages
set {year:y, month:m, day:d} to date sent of theMessage
set theDate to (y * 10000 + m * 100 + d) as string
set theAttachments to every mail attachment of theMessage
repeat with b from 1 to length of theAttachments
set theAttachment to item b of theAttachments
set theAttachmentName to theDate & " " & name of theAttachment
set theSavePath to theOutputFolder & theAttachmentName
tell application "System Events" to exists file theSavePath
repeat while the result
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set delimitedList to every text item of theSavePath
set suffix to "." & last item of delimitedList
try
copy text items 1 thru -2 of theSavePath to theSavePathBase
on error
copy theSavePath to theSavePathBase
end try
-- display dialog "theSavePath pre- : " & theSavePath
-- display dialog "theSavePathBase & ' copy' & suffix pre- " & theSavePathBase & " copy" & suffix
set AppleScript's text item delimiters to oldDelims
copy theSavePathBase & " copy" & suffix to theSavePath
-- display dialog "theSavePath post- : " & theSavePath
tell application "System Events" to exists file theSavePath -- <<< *** BOMBS HERE
-- display dialog "Made it past existence check."
end repeat
try
display dialog "preparing to save..."
save theAttachment in theSavePath
on error errText number errNum
display dialog errText
end try
end repeat
end repeat
end tell
It works, except that when the need to fix the name of a copy is encountered, it bombs at the indicated location with this message: "System Events got an error: Can’t make {"...Desktop:Mail Attachments:20140830 Resumé 2014", "pages", " copy", ".zip"} into type integer." Something about the way I reconstituted the name changed the type to something the exists checker can't handle.
Any help would be appreciated. Help with an explanation would be better, since I'm not an AppleScripter but am interested. Thanks!
Your error message is telling you the problem. Notice the brackets {} around the error message.
Can’t make {"...Desktop:Mail Attachments:20140830 Resumé 2014", "pages", " copy", ".zip"} into type integer."
That's indicating that this is a list of items, not a string. As such you need to make it a string first therefore change...
copy text items 1 thru -2 of theSavePath to theSavePathBase
to:
copy (text items 1 thru -2 of theSavePath) as text to theSavePathBase
With that being said, I don't think your actual copy command (as follows) will work. It seems you're wanting the copy command to rename the file and copy it all in one step.
copy theSavePathBase & " copy" & suffix to theSavePath
The copy command won't be able to find "...Desktop:Mail Attachments:20140830 Resumé 2014.pages. copy.zip" because it doesn't exist. That's the name you want for the renamed file. I think you're best approach would be to use the "cp" unix executable command to preform your copy because it can copy and rename in one step. Something like this although you'll have to figure out how to get the actual value for the variable "theAttachmentCurrentPath".
do shell script "cp " & quoted form of POSIX path of theAttachmentCurrentPath & space & quoted form of POSIX path of (theOutputFolder & text 1 thru -5 of theAttachmentName & " copy" & text -4 thru -1 of theAttachmentName)
i'm writing a script that needs to copy and paste text from an entry in a dialogue box
set query to text returned of (display dialog "Enter Text" default answer "" buttons {"Input", "Cancel"} default button 1)
query
tell application "System Events"
keystroke "c" using command down
keystroke "v" using command down
end tell
when i run this script it copies and pastes "set query to text..."
how can I copy the query that I entered ? i'm going to paste the text somewhere else later but I need to figure out how to actually copy the text. I was using keystroke query but that was taking a long time when the strings were long
Use set the clipboard:
display dialog "" default answer ""
set the clipboard to text returned of result
If others get here searching for how to for example copy a long error message from a dialog shown by OS X, you can use Accessibility Inspector, which is in /Applications/Xcode.app/Contents/Applications/.