AppleScript - finding creation/modification dates for files - date

I have simple service I'm using to import Evernote files:
on run {input}
tell application "Evernote"
repeat with x in input
try
create note from file x
on error error_message number error_number
display alert "Send to Evernote Failed" message "Error: " & error_message & "" & "Error Number: " & error_number as warning
end try
end repeat
end tell
end run
I would like to "create note from file x created d", where d is a date/time that matches that of the original file - but I don't know how to get the date/time (either created or modified) of the original file in a format that is an applescript date.
I appreciate the help!

I don´t know where you´re getting your "input" from, but assuming that with "files" you literally mean files int the Finder, calling Finder to get info on the file in "input" should work like this:
tell application "Finder" to set input to selection
repeat with x in input
tell application "Finder" to set creationDate to creation date of x
end repeat

Related

Why isn't a variable being set unless the script is run within Script Debugger's step debugger?

I have the following AppleScript that works fine when I step through each line one at a time using Script Debugger, but reports that the _doc variable has a missing value when it gets the the save as line.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
set _folder to choose folder
set _files to files of _folder
repeat with _file in _files
if creator type of _file is "MSWD" then
tell application "Microsoft Word"
open _file
set _doc to document of window 1
save as _doc file format format text
close _doc
end tell
end if
end repeat
end tell
I've tried pausing for as long as 5 seconds using delay 5 with no change in the behavior. Why might this be happening and what can I do about it?
The answer to "why might this be happening" seems to be "it probably is a timing problem" and "because there are a number of problems when automating Word from both AppleScript and VBA on Mac that Microsoft has not yet fixed". I don't think there's much you can do about it except report to Microsoft via the Smiley mechanism or via word.uservoice.com. On uservoice, best to add your vote than existing request if there is one. But there is no reason at all to believe that Microsoft will even acknowledge or fix quite serious automation problems at the moment.
I had not come across the problem where you couldn't even set _doc to document of window 1 successfully. I was always able to use
set _doc to open _file
Here, I found a "delay 5" was enough to solve the problem you report, but there has also long been a problem where the "_doc" variable becomes invalid after a save as. I had a solution to that that iterates through the windows, so have put together this script which
a. should reduce the delay as much as possible
b. works on simple test data here but could do with improvement, especially on error checking
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
set _folder to choose folder
set _files to files of _folder
repeat with _file in _files
if creator type of _file is "MSWD" then
tell application "Microsoft Word"
--activate
set doc_count to (count of documents)
open _file
-- You have to set the maximum no. of repeats
-- high enough for your system
set repeats to 50
repeat until (count of documents) > doc_count or repeats = 0
set repeats to repeats - 1
end repeat
if (count of documents) > doc_count then
set _doc to (document (doc_count + 1))
set _windows to the windows
repeat with _window in _windows
if the full name of the document of _window is the full name of _doc then
set _windowIndex to the entry_index of _window
exit repeat
end if
end repeat
-- you need to create a new file name for each file.
-- this is a temporary kludge
set _textfilename to (posix full name of _doc) & ".txt"
save as _doc file name _textfilename file format format text
-- _doc now invalid, we need to "reconnect"
set _windows to the windows
repeat with _window in _windows
if the entry_index of _window is _windowIndex then
set _doc to the document of _window
exit repeat
end if
end repeat
close _doc saving no
else
-- you can make this more informative, and you might still need to
-- try to close something.
display dialog "Could not open document: " & POSIX path of _file
end if
end tell
end if
end repeat
end tell
Incidentally, when I tested on a single document using open recent file instead, there was never any problem getting a reference to the document. But that's useless for the kind of thing you're trying to do.

applescript - copy paste sender's email address to numbers

is it possible that when a new email message is received to run applescript to copy sender’s email address to clipboard and paste it to next empty cell in column e in numbers, I've been trying with this, but it doesn't select the first message
thanks in advance :)
property workFlowPath : quoted form of ("/Users/pl 1/Library/Application Scripts/com.apple.mail/accepted.workflow") --CHANGE THIS TO YOUR FULL WORKFLOW FILE PATH example "/Users/pl 1/Library/Application Scripts/com.apple.mail/accepted.workflow"
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with eachMessage in theMessages
(*Get the email Subject *)
set input_Argument to subject of eachMessage
(*Run subroutine for workflow *)
my runAutomator(input_Argument)
end repeat
end perform mail action with messages
end using terms from
(*Workflow Subroutine *)
on runAutomator(input_Argument)
(*Path to workflow *)
(*Setup and Run the unix command *)
set command to "/usr/bin/automator -i " & quoted form of input_Argument & space & workFlowPath
do shell script command
end runAutomator
on run {input, parameters}
tell application "Numbers"
tell application "Finder"
set theFile to POSIX file “/Users/pl 1/Google Drive/Publication Submission Tracker2.numbers"
open theFile
end tell
end tell
return input
end run
________pause for 5 seconds______
tell application "Mail"
reopen
activate
set theSenderList to {}
set theMessages to the first message of message viewer 0
repeat with aMessage in theMessages
set oneAddress to extract address from sender of aMessage
set end of theSenderList to oneAddress
end repeat
set the clipboard to (theSenderList as string)
end tell
return
end run
________pause for 5 seconds______
tell application "Numbers"
reopen
activate
tell application "System Events" to tell process "Numbers"
set currentCell to "e2"
keystroke "v" using {command down}
end tell
end tell
end run
It is best not to use UI scripting here. You want to set this up as a trigger in mail.app. Your script can pull the sender email address and then programmatically add it to the first empty cell in a row. Here's a script to get you started:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application id "com.apple.iWork.Numbers"
activate
-- can also tell Numbers to open a specific document
end tell
tell application "Mail"
repeat with eachMessage in theMessages
set fromAddress to extract address from reply to of eachMessage
tell application id "com.apple.iWork.Numbers"
tell document 1
tell the active sheet
tell table 1
tell column "E" -- change to preferred column
-- find the last empty row in this column
set r to (address of row of first cell whose value is missing value)
set value of cell r to fromAddress
set r to r + 1
end tell
end tell
end tell
end tell
end tell
end repeat
end tell
end perform mail action with messages
end using terms from
Edit: I edited the script to check for the next cell with a 'missing value' (instead of the cell value > 0) so that it can handle numbers and dates, such as:
set value of cell r to date "12/5/15"

AppleScript iPhoto import command (how to deal with duplicates)

I'm trying to fully automate a workflow for getting images into iPhoto. The last piece of the puzzle is handling duplicate images. Currently I see no way in Applescripts import command to not import duplicates. Therefore when a duplicate arrises all workflow stops and iPhoto waits for input.
Is there a way to get through this?
You will need to script this. Here is a bit of code that seems to work inside applescript editor. Adjust accordingly for automator...
set iFolder to (choose folder)
set iFiles to (list folder iFolder)
tell application "iPhoto"
repeat with iFile in iFiles
try
set pFound to get (every photo of album "Photos" whose image filename is iFile)
end try
if length of pFound is not 0 then
log ("File '" & iFile as text) & "' exists..."
# Move or delete it here
end if
end repeat
# Continue with import
import from (iFolder as alias)
end tell

Automating editing SystemVersion.plist

I am currently using OSX10.10 and need to use MATLAB; however because they haven't updated the application to support 10.10 it will crash on launch.
Up until now I have been using pico to edit the SystemVersion.plist [1] (changing the version from 10.10 to 10.9); and that works great except that it is really annoying to edit the file every time I need to open MATLAB and re-edit it every time I close MATLAB.
What I want to do is when I start the script it will edit the SystemVersion.plist to the correct version so that I can run MATLAB without it crashing; and then when MATLAB exits it resets the version back from 10.9 to 10.10). I have a bit of code (which may be poorly written; I have never used applescript before);
tell application "System Events"
set ProcessList to name of every process
if "MATLAB" is in ProcessList then
tell application "System Events"
tell property list file "/System/Library/CoreServices/SystemVersion.plist"
tell contents
set value of property list item "ProductUserVisibleVersion" to "10.9"
set value of property list item "ProductVersion" to "10.9"
end tell
end tell
end tell
else
tell application "System Events"
tell property list file "/System/Library/CoreServices/SystemVersion.plist"
tell contents
set value of property list item "ProductUserVisibleVersion" to "10.10"
set value of property list item "ProductVersion" to "10.10"
end tell
end tell
end tell
end if
end tell
[1] - Error trying to installing JDK8 U11 OSX 10.10 Yosemite
I had the same approach, but came down to this solution: (for os x yosemite and matlab r2014a)
tell application "System Events"
set plistFile to property list file "/System/Library/CoreServices/SystemVersion.plist"
tell plistFile
get property list item "ProductVersion"
set value of property list item "ProductVersion" to "10.90"
end tell
end tell
do shell script "export MATLAB_USE_USERWORK=1" & ";/Applications/MATLAB_R2014a.app/bin/matlab -desktop &> /dev/null &"
display dialog "..." buttons {"Ok"} with icon note giving up after 10
tell application "System Events"
set plistFile to property list file "/System/Library/CoreServices/SystemVersion.plist"
tell plistFile
get property list item "ProductVersion"
set value of property list item "ProductVersion" to "10.10"
end tell
end tell
the dialog box is needed. delay (in seconds) does not do it for any reasons (i first used applescript to solve the matlab problem). there might be another solution, but this works for me.
if you are using a mac with retina display, you may want to install an java 7 runtime environment and replace the do shell script part with the following:
do shell script "export MATLAB_JAVA=\"/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home\"" & "; export MATLAB_USE_USERWORK=1" & ";/Applications/MATLAB_R2014a.app/bin/matlab -desktop &> /dev/null &"
the icons still do look a little shitty, but the fonts aren't blurry anymore.
i hope this may help anybody facing the problem after updating to yosemite lately.
jens

Applescript: "set user interaction level to never interact" not working in illustrator

I'm trying to set it so that there's no user interaction when I open up my illustrator file using applescript, but the standard:
tell application id "com.adobe.Illustrator"
activate
set user interaction level to never interact
open theFile without dialogs
doesn't work for this plugin I have installed that checks for white overprints.
If it were up to me I'd just uninstall the plugin but it's for a work pc.
I also tried clicking the button automatically (with help from Tim Joe) by using:
try
tell application "System Events"
tell process "Finder"
click button "OK" of window "Adobe Illustrator"
end tell
end tell
end try
and I've tried
tell application "System Events"
tell process "Adobe Illustrator"
keystroke return
end tell
end tell
Does anyone know a way of solving this?
below is the full code as it currently stands:
set saveLocation to ((path to desktop) as string) --place to save the files
set theFile to choose file with prompt "Choose the Illustrator file to get outlines on"
set outputFolder to choose folder with prompt "Select the output folder"
tell application "Finder" to set fileName to name of theFile
set fullPath to (saveLocation & fileName) --file path of new .ai
set fileName to (text 1 thru ((length of fileName) - 3) of fileName) --remove .ai from fileName
set olPath to text 1 thru ((length of fullPath) - 3) of fullPath & "_OL.ai" --path of outlined file
tell application id "com.adobe.Illustrator"
activate
ignoring application responses
open theFile without dialogs
end ignoring
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
save current document in file fullPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save file to desktop
convert to paths (every text frame of current document) --convert text to paths
save current document in file olPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save another copy to desktop with name + _OL.ai
end tell
tell application "Finder"
set newFolder to make new folder at saveLocation with properties {name:fileName}
move fullPath to newFolder --create new folder and move both new files into it
move olPath to newFolder
set newFolderPath to (newFolder) as string
set newFolderPath to text 1 thru -2 of newFolderPath --remove the trailing ":"
tell current application --zip up the new folder
set qpp to quoted form of POSIX path of newFolderPath
do shell script "cd $(dirname " & qpp & ")
zip -r \"$(basename " & qpp & ").zip\" \"$(basename " & qpp & ")\""
end tell
set zipFile to newFolderPath & ".zip"
move zipFile to outputFolder --move .zip to output
delete newFolder --delete folder on desktop left from zipping
end tell
--prepare a notification email
set presetText to "Hello,
Files Uploaded:
" & fileName & ".zip
To access our FTP Server:
http://217.207.130.162:8080/WebInterface/login.html
To access our FTP server, log onto our website below:
Username:
Password:
Thanks,
Joe"
tell application "Mail" --open up prepared email
activate
set theMEssage to make new outgoing message with properties {visible:true, subject:fileName, content:presetText}
end tell
--open file containing usernames and passwords for the FTP
do shell script "open /Users/produser/Desktop/FTP_Users"
I tracked down and installed White Overprint Detector I could see what you mean. I had to use an older version as I only have CS3, and I saw the dialog it produces when you open a document. The following worked for me to get it to dismiss:
tell application "Adobe Illustrator" to activate
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
Since my original post seemed too objective to understand I will revise.
With in the tell block for illustrator look for your line that opens the file. Some commands allow with and without properties. Try applying the "without dialogs" property to look something like this.
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
Update:
Two work arounds I can think of. 1) Try telling system events to tell AI to open without dialogs
tell application "system events"
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
end tell
Other is just add in a bit that will just okay the prompt.
try
tell application "System Events"
tell process "Finder"
click button "Continue" of window "Adobe Illustrator"
end tell
end tell
end try
Can try just having it accept the default button.
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell