Assigning a picture to a new Outlook contact via Applescript - outlook

How would I go about assigning a picture to a new Outlook contact in Applescript? The Outlook dictionary tells me that Outlook does have an image class, but I have no clue how to populate one. It doesn't seem to like just being given what the finder thinks is a Jpeg file:
tell application "Finder"
set theImageFile to a reference to file "Macintosh HD:Users:maximiliantyrtania:Pictures:image-151718-galleryV9-zhek.jpg"
--set theImageFilePosixPath to POSIX file "/Users/maximiliantyrtania/Pictures/image-151718-galleryV9-zhek.jpg"
set filekind to (kind of theImageFile) as string
display dialog "its a" & filekind--displays "its a jpeg"
end tell
tell application "Microsoft Outlook"
make new contact with properties {first name:"some", last name:"one", image:theImageFile, email addresses:{{address:"work#mywork.com", type:work}, {address:"home#myhome.com", type:home}}}
end tell
Result: error "„«class docf» \"image-151718-galleryV9-zhek.jpg\" of «class cfol» \"Pictures\" of «class cfol» \"maximiliantyrtania\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\"“ kann nicht in den erwarteten Typ umgewandelt werden." number -1700 from «class docf» "image-151718-galleryV9-zhek.jpg" of «class cfol» "Pictures" of «class cfol» "maximiliantyrtania" of «class cfol» "Users" of «class sdsk»
I guess I have to say something along the lines of:
set theImageToAssign to make new image () , but what might the syntax be? Can't find anything about that on the net.

You can load the image bytes directly, and then pass them in after the contact is created.
set theImageFile to a reference to POSIX file "/Users/maximiliantyrtania/Pictures/image-151718-galleryV9-zhek.jpg"
set img_file to open for access theImageFile
set img_data to read img_file as "JPEG"
tell application "Microsoft Outlook"
set new_contact to make new contact with properties {first name:"Joan", last name:"Smith", email addresses:{{address:"work#mywork.com", type:work}, {address:"home#myhome.com", type:home}}}
set the image of new_contact to img_data
end tell
Would be good practice trap the above image reading stuff for errors too.

Related

AppleScript error Can't make path into type alias

I’m trying to set up an AppleScript that will get a path from a txt file(mockUPPath.txt) on the Desktop which in theory will allow me to attach a jpg. The name of the folder and jpg will change so I can’t hard code it. I also get the body of the email from another text file on the Desktop which I managed to get working. I can get it to build the email but not find and attach the jpg.
The text in the mockUPPath.txt file looks like this: Name_Mock-up/555.jpg.
The jpg 555.jpg is in a folder “Name_Mock-up” on the Desktop
This is what I have so far (below), anyone know where I’m going wrong?
set AppleScript's text item delimiters to return
set theFile to ((path to desktop as text) & "mockUPText.txt")
set theData to paragraphs of (read file theFile)
set myPath to ((path to desktop as text) & "mockUPPath.txt")
set thePath to paragraphs of (read file myPath)
set newPath to (POSIX path of (path to desktop folder)) & thePath
--Email
set theMessageSubject to "Test email"
set theRecipient to "email#test.com"
set theMessageContent to theData
set theMessageAttachment to newPath
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:theMessageSubject, content:theMessageContent & linefeed & linefeed}
tell theMessage
make new to recipient at end of to recipients with properties {address:theRecipient}
make new attachment with properties {file name:theMessageAttachment as alias} at after the last word of the last paragraph
end tell
end tell
Based on the information you presented in your OP, the following example AppleScript code works for me as tested in Script Editor.
First I did the following:
Created a folder on my Desktop named Name_Mock-up containing a screenshot of the example AppleScript code in Script Editor naming it 555.jpg
Created a file on my Desktop named mockUPText.txt with a single line of text, Name_Mock-up/555.jpg.
The ran the example AppleScript code:
set theFile to ((path to desktop as text) & "mockUPText.txt") as alias
set theData to read theFile
set myPath to theFile
set thePath to read myPath
set newPath to (POSIX path of (path to desktop folder)) & thePath
set theMessageSubject to "Test email"
set theRecipient to "email#test.com"
set theMessageContent to theData
set theMessageAttachment to newPath
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:theMessageSubject, content:theMessageContent & linefeed & linefeed}
tell theMessage
make new to recipient at end of to recipients with properties {address:theRecipient}
make new attachment with properties {file name:theMessageAttachment} at after the last word of the last paragraph
end tell
end tell
Here is a screenshot of the result of running the example AppleScript code:
Note: That all said, I would not necessarily code it in this manner and have only presented this to correct some of the code you originally posted as mentioned in part by my comment to your OP.
First of all the most reliable way to interact with read/write API is to coerce the path to «class furl» and delete the file keyword.
Second of all most of the text files are UTF 8 encoded. If so read it as «class utf8»
The error occurs because you cannot coerce a POSIX path to alias. You need an extra step
Text item delimiters makes no sense, read the body text as one string and get the first item of the thePath.
set theFile to ((path to desktop as text) & "mockUPText.txt") as «class furl»
set theData to (read theFile as «class utf8»)
set myPath to ((path to desktop as text) & "mockUPPath.txt") as «class furl»
set thePath to paragraphs of (read myPath as «class utf8»)
set newPath to (POSIX path of (path to desktop folder)) & item 1 of thePath
set newAlias to POSIX file newPath as alias -- if the file doesn't exist you'll get an error
--Email
set theMessageSubject to "Test email"
set theRecipient to "email#test.com"
set theMessageContent to theData
set theMessageAttachment to newAlias
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:theMessageSubject, content:theMessageContent & linefeed & linefeed}
tell theMessage
make new to recipient at end of to recipients with properties {address:theRecipient}
make new attachment with properties {file name:theMessageAttachment} at after the last word of the last paragraph
end tell
end tell

What file is being moved in applescript

Good Day.
I have a basic applescript that is moving the oldest file from one folder to another. I'm running the applescript from the command line:
osascript /Users/dmayo/Documents/scripts/MoveOldest.scpt
I'm also having the script stdout to the terminal, but can't seem to reference just the filename. Here's what I get at the line in code log this_item:
«class docf» 2016-04-12-01-31-31.pdf of «class cfol» 2013-long of «class cfol» Scans of «class cfol» dmayo of «class cfol» Users of «class sdsk»
I'd like just the file name "2016-04-12-01-31-31.pdf" on each line of the output. Here's my Applescript:
repeat
tell application "Finder"
set src to folder "Macintosh HD:Users:dmayo:Scans:2013-long"
set dest to folder "Macintosh HD:Users:dmayo:Scans"
set sorted_list to sort items of src by creation date
set this_item to item 1 of sorted_list
move item 1 of sorted_list to dest
log this_item
end tell
delay 120
end repeat
Thanks.
Just log the name of the item
Edit: As log does not belong to the Finder move it out of the Finder tell block
repeat
tell application "Finder"
set src to folder "Macintosh HD:Users:dmayo:Scans:2013-long"
set dest to folder "Macintosh HD:Users:dmayo:Scans"
set sorted_list to sort items of src by creation date
set this_item to item 1 of sorted_list
move this_item to dest
set fileName to name of (this_item as alias)
end tell
log fileName
delay 120
end repeat

Applescript Handler

I am using the below script for few months. It ask the user to select from the list and copy paste the text in MS Word and run some VB Macro and save the file as Text file.
tell application "Finder"
if not (exists folder "Test" of desktop) then make new folder at desktop with properties {name:"Test"}
end tell
set desktopTestFolder to (path to desktop folder as text) & "Test:"
set mychoice to (choose from list {"PS List", "AA Table", "PS Legend", "PO Chart", "MD"} with prompt "Please select which sound you like best" default items "None" OK button name {"Play"} cancel button name {"Cancel"})
if mychoice is false then error number -128 -- user canceled
tell application "Microsoft Word"
set theContent to content of text object of selection
copy object text object of selection
set newDoc to make new document
delay 2
tell application "System Events"
tell process "Microsoft Word"
keystroke "v" using command down
end tell
end tell
run VB macro macro name "Normal.NewMacros.Clean"
run VB macro macro name "Normal.Module9.bold"
save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
close document 1 saving no
end tell
But when I try it to put in an handler it not works. What I have tried is:
tell application "Finder"
if not (exists folder "Test" of desktop) then make new folder at desktop with properties {name:"Test"}
end tell
set desktopTestFolder to (path to desktop folder as text) & "Test:"
set mychoice to (choose from list {"PS List", "AA Table", "PS Legend", "PO Chart", "MD"} with prompt "Please select which sound you like best" default items "None" OK button name {"Play"} cancel button name {"Cancel"})
if mychoice is false then error number -128 -- user canceled
set mychoice to mychoice as text
if mychoice is equal to "PS List" then
handler1()
else
handler2()
end if
on handler1()
tell application "Microsoft Word"
set theContent to content of text object of selection
copy object text object of selection
set newDoc to make new document
delay 2
tell application "System Events"
tell process "Microsoft Word"
keystroke "v" using command down
end tell
end tell
run VB macro macro name "Normal.NewMacros.EDCleanup1"
run VB macro macro name "Normal.Module9.bold"
save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
close document 1 saving no
end tell
end handler1
on handler2()
tell application "Microsoft Word"
run VB macro macro name "Normal.NewMacros.EDCleanup1"
run VB macro macro name "Normal.Module9.bold"
save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
close document 1 saving no
end tell
end handler2
Please let me know, Where I am wrong?
Thanks
Josh
You didn't say what result you get when you say "it not works". Do you get an error? Do you get nothing? Do you get the same result when you select "PS List" versus the others?
The error I see is that you never bring Microsoft Word to the front. This is necessary for when you're using UI scripting and the keystroke command. Add 'activate' to your Word tell blocks.
tell application "Microsoft Word"
activate
set theContent to content of text object of selection
…
Also, yes, your variable desktopTestFolder loses scope. You can make the variable a global property by placing this at the front of your script:
property desktopTestFolder: ""

Create external file reference in FileMaker Pro with applescript

I have a FileMaker Pro 12 database with an external file container field. I'd like to use an applescript to create records and populate this field automatically. I've tried several things already, but I get the corresponding errors.
set theFilePath to choose file with prompt "Please select your file:"
tell application "Finder"
set theFile to file theFilePath
end tell
tell application "FileMaker Pro"
set theRecord to create new record at database "MyDatabase"
tell theRecord
set cell "file" to theFile
end tell
end tell
Results in:
error "Can’t make «class docf» \"movie.MOV\" of «class cfol» \"compressed\" of «class cdis» \"Drobo\" of application \"Finder\" into the expected type." number -1700 from «class docf» "movie.MOV" of «class cfol» "compressed" of «class cdis» "Drobo"
Changing the set line either of these:
set cell "file" to theFilePath
set cell "file" to (theFile as alias)
Results in:
error "FileMaker Pro got an error: Can’t set cell \"file\" of record ID 276.0 of table \"MyDatabase\" of database \"MyDatabase.fmp12\" to alias \"Drobo:compressed:movie.MOV\"." number -10006 from cell "file" of record ID 276.0 of table "MyDatabase" of database "MyDatabase.fmp12"
FileMaker 12 uses a special string path format for external file containers. It's similar to Posix but with a custom protocol identifier and the drive name.
eg,
filemac:/MacintoshHD/Users/JohnSmith/Documents/test.xlsx (see here for more info)
Give this modified script a go, it uses absolute (full) paths.
set theFilePath to choose file with prompt "Please select your file:"
set theFileRef to makeFMPExternalFileRef(theFilePath)
tell application "FileMaker Pro Advanced"
set theRecord to create new record at database "MyDatabase"
tell theRecord
set cell "file" to theFileRef
end tell
end tell
on makeFMPExternalFileRef(fromFile)
tell application "Finder" to return "filemac:/" & (the name of the disk of fromFile) & (the POSIX path of fromFile)
end makeFMPExternalFileRef
I wasn't able to do this directly with applescript. But I did accomplish it with a combination of applescript and a file maker script.
I use applescript to create a file-maker-style file path in a separate text field. The file maker script then inserted the matching file into a container field. So my applescript looks, roughly, like this:
set theFilePath to choose file with prompt "Please select your file:"
tell application "Finder"
set theFile to file theFilePath
end tell
tell application "FileMaker Pro"
set theRecord to create new record at database "MyDatabase"
tell theRecord
set cell "filePath" to "filemac:" & POSIX path of theFile
end tell
do script "File Linker"
end tell
The corresponding file maker script is (the container field is called "file"):
Go to Record/Request/Page [First]
Loop
If [ not MyDatabase::file]
Set Variable [$filePath; Value:MyDatabase::filePath]
Insert File [MyDatabase::file; "$filePath"]
End If
Go to Record/Request/Page [Next; Exit after last]
End Loop
Works like a charm.

Applescript: Getting POSIX paths to all the files in a folder

I'm trying to script the opening of media files in Wirecast.
I want to open all of the files in a specific folder as Wirecast "shots".
According to the Wirecast Dictionary, the applescript command syntax to add a shot is:
AddShotWithMedia layer with posix_path anything
I can't figure out how to either get a list of posix paths to all the files in a folder, or alternatively, convert an applescript path to a posix path acceptable to Wirecast.
Here's the code:
tell application "Finder"
set SettingsFolder to folder "WirecastMedia" of home
set MediaFolder to folder "Titles" of SettingsFolder
set TitleList to entire contents of MediaFolder
end tell
tell application "Wirecast"
activate
set myFile to (file "KorgWirecast.wcst" of SettingsFolder)
open myFile as alias
set myDoc to last document
set myLayer to the layer named "Titles" of myDoc
repeat with aFile in TitleList
AddShotWithMedia myLayer with posix_path aFile
end repeat
end tell
...it fails on the AddShotWithMedia line with the message:
Can’t make «class docf» "ManyVoicesSuper.png" of «class cfol» "Titles"
of «class cfol» "WirecastMedia" of «class cfol» "ram" of «class cfol»
"Users" of «class sdsk» of application "Finder" into the expected type.
file "ManyVoicesSuper.png of folder "Titles" ... of application "Finder" is a Finder reference to the file. What you need is a string in the form of a POSIX path. I notice you're using entire contents of the folder, which includes subfolders, but if you only need to get files out of the top folder, you can use System Events like this:
tell application "System Events"
set TitleList to POSIX path of disk items of folder "~/WirecastMedia/Titles"
end tell

Resources