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

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

Related

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 find folder with specific file

I have a folder with file /resources/video.mov. This folder with video can located in any place, like /Users/UserName/Desktop/resources/video.mov or /Applications/resources/video.mov and etc. I need to find this folder with video and copy to specific place. I can't find path of folder with video to copy it.
tell application "Finder"
duplicate items in folder "_locatedPath_" to folder "_destiontionPath_" with replacing
end tell
This works for me using the latest version of Sierra
Change the value of variable destinationFolder to the output folder of your choice
property theContainer : missing value
property containingFolder : "resources" -- change if needed
property theSearch : "video.mov" -- change if needed
property destinationFolder : (path to desktop as text) -- change if needed
set findFile to do shell script "mdfind -name " & theSearch
repeat with i from 1 to number of paragraphs in findFile
set this_item to POSIX file (paragraph i of findFile) as alias
tell application "Finder"
if name of this_item is theSearch then
set theContainer to the container of this_item as text
if theContainer contains containingFolder then
set resultObject to duplicate this_item ¬
to destinationFolder ¬
replacing true ¬
routing suppressed true ¬
with exact copy
end if
end if
end tell
end repeat

Assigning a picture to a new Outlook contact via Applescript

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.

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.

Reading file in AppleScript

I'm trying to read an html file into a variable in AppleScript, I have the following code.
tell application "Finder"
set theItems to every file of folder folderName
repeat with theFile in theItems
open for access theFile
set fileContents to (read theFile)
end repeat
end tell
Now I get an error like:
Finder got an error: Can’t make document file "index.html" of folder
[...] of startup disk into type «class fsrf».
What am I doing wrong? I followed this example. Are HTML files not recognized as text?
You have to convert the Finder file objects to aliases or text.
read can be used without separate open or close commands. It reads files as MacRoman without as «class utf8» though. (as Unicode text is UTF-16.)
tell application "Finder" to files of folder "HD:Users:lauri:Sites" as alias list
repeat with f in result
read f as «class utf8»
end repeat
Try:
tell application "Finder" to set theItems to every file of folder folderName
repeat with theFile in theItems
set aFile to POSIX path of (theFile as text)
set fileContents to do shell script "cat " & quoted form of aFile
end repeat
Starting from your original code, this should do it:
set folderPath to choose folder
set someData to ""
tell application "Finder"
set theItems to every file of folder folderPath as list
repeat with theFile in theItems
set theFilePath to theFile as text
if characters -5 thru -1 of theFilePath as string is ".html" then
set theFileHandle to (open for access file theFilePath)
set fileContents to (read theFileHandle)
-- for testing, call some function
set someData to someData & return & processHtml(fileContents) of me
close access theFileHandle
end if
end repeat
-- do something with someData here
return someData
end tell
on processHtml(theData)
-- do something with theData here
return theData
end processHtml
As Lauri wrote, you can add "as «class utf8»" to read the file as UTF8. You could also use "as Unicode text" for UTF16. Personally, I like this, because it is vanilla AppleScript and doesn't need shell scripting.
Using open for access is really doing it the hard way.
If you want to read an HTML file with AppleScript, then the best way to do that is to use AppleScript to tell an HTML editor to read the HTML file for you. That is the fundamental way that AppleScript works. That’s why “tell” is the most important command. That’s why you can accomplish your goal of reading an HTML file into a variable in just 3 lines:
tell application "BBEdit"
open (choose file)
set theHTMLSource to the text of document 1
close document 1
end tell
The following script expands on the above to read an arbitrary number of HTML files from a chosen folder. It works with BBEdit 9, and should also work with BBEdit’s free version, which is called “TextWrangler” and is available in Mac App Store. Or you can fairly easily adapt this script for use with HyperEdit or TextEdit or whatever AppleScript-aware HTML/text editor you prefer to use.
tell application "Finder"
set theFolder to (choose folder)
set theFiles to every file of folder theFolder
set theHTMLSourceList to {}
repeat with theFile in theFiles
if the kind of theFile is equal to "HTML document" then
set theName to the name of theFile
tell application "BBEdit"
open file (theFile as text)
set theSource to the text of document 1
copy {theName, theSource} to the end of theHTMLSourceList
close document 1
end tell
end if
end repeat
end tell
When the above script is finished, the variable “theHTMLSourceList” is populated with the names and source code of the entire folder of HTML documents, like so:
{{name of file 1, source of file 1}, {name of file 2, source of file 2}, {name of file 3, source of file 3}}
… and so on up to an arbitrary number of files. But of course you can have the script return the HTML source to you in whatever way you like. The key point is that an AppleScript-aware HTML editor can both read HTML and set AppleScript variables, so you don’t have to write (and debug and maintain) your own HTML reader in tiny AppleScript.

Resources