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.
Related
I was looking for a script that would batch convert all *.numbers files in a given folder to *.csv files.
I found the following on GitHub and added an additional line as suggested in the comments suggestion. When I run the script, Numbers launches and opens the test file from the folder specified - but the file is not exported. Numbers just stays open and terminal errors out with:
/Users/Shared/Untitled.scpt: execution error: Numbers got an error: Invalid key form. (-10002)
The script (located in /Users/Shared) has the following permissions:
-rwxr-xr-x
#!/usr/bin/osascript
on run argv
set theFilePath to POSIX file (item 1 of argv)
set theFolder to theFilePath as alias
tell application "Finder" to set theDocs to theFolder's items
-- Avoid export privilege problem
set privilegeFile to (theFolder as text) & ".permission"
close access (open for access privilegeFile)
repeat with aDoc in theDocs
set docName to aDoc's name as text
if docName ends with ".numbers" then
set exportName to (theFolder as text) & docName
set exportName to exportName's text 1 thru -9
set exportName to (exportName & "csv")
tell application "Numbers"
open aDoc
delay 5 -- may need to adjust this higher
tell front document
export to file exportName as CSV
close
end tell
end tell
end if
end repeat
end run
Any suggestions?
Here is what I did and works for me in macOS High Sierra:
In Terminal:
touch numb2csv; open -e numb2csv; chmod +x numb2csv
• This creates an empty ASCII Text file named numb2csv.
• Opens, by default, numb2csv in TextEdit.
• Makes the numb2csv file executable.
Copy and paste the example AppleScript code, shown further below, into the opened numb2csv file.
Save and close the numb2csv file.
In Terminal executed the numb2csv executable file, e.g.:
./numb2csv "$HOME/Documents"
This created a CSV file of the same name as each Numbers document in my Documents folder, not traversing any nested folders.
Example AppleScript code:
#!/usr/bin/osascript
on run argv
set theFilePath to POSIX file (item 1 of argv)
set theFolder to theFilePath as alias
tell application "System Events" to set theDocs to theFolder's items whose name extension = "numbers"
repeat with aDoc in theDocs
set docName to aDoc's name as text
set exportName to (theFolder as text) & docName
set exportName to exportName's text 1 thru -8
set exportName to (exportName & "csv")
tell application "Numbers"
launch
open aDoc
repeat until exists document 1
delay 3
end repeat
tell front document
export to file exportName as CSV
close
end tell
end tell
end repeat
tell application "Numbers" to quit
end run
NOTE: As coded, this will overwrite an existing CSV file of the same name as each Numbers file processed, if they already exist. Additional coding required if wanting to not overwrite existing files
If you receive the Script Error:
Numbers got an error: The document “name” could not be exported as “name”. You don’t have permission.
It is my experience that the Numbers document was not fully opened prior to being exported and that increasing the value of the delay command resolves this issue. This is of course assuming that one actually has write permissions in the folder the target Numbers documents exists.
Or one can introduce an error handler within the tell front document block which, if my theory is right about the target document not being fully loaded before the export, will give additional time, e.g.:
Change:
tell front document
export to file exportName as CSV
close
end tell
To:
tell front document
try
export to file exportName as CSV
close
on error
delay 3
export to file exportName as CSV
close
end try
end tell
Note: The primary example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. See included example directly above.
I was looking for that, unfortunately, that doesn’t work anymore.
This line
tell application "System Events" to set theDocs to theFolder's items whose name extension = "numbers"
Gets the following error:
execution error: Can’t make file "file.numbers" of application "System Events" into the expected type. (-1700)
macOs Big Sur Versio 11.01
automator version 2.10
Numbers version 10.3.5
Inspired by this thread and those articles Exporting Numbers Documents and Get full directory contents with AppleScript
The following code works:
#!/usr/bin/osascript
log "Start"
property exportFileExtension : "csv"
tell application "Finder"
activate
set sourceFolder to choose folder with prompt "Please select directory."
set fileList to name of every file of sourceFolder
end tell
set the defaultDestinationFolder to sourceFolder
repeat with documentName in fileList
log "documentName: " & documentName
set fullPath to (sourceFolder as text) & documentName
log "fullPath: " & fullPath
if documentName ends with ".numbers" then
set documentName to text 1 thru -9 of documentName
tell application "Finder"
set newExportItemName to documentName & "." & exportFileExtension
set incrementIndex to 1
repeat until not (exists document file newExportItemName of defaultDestinationFolder)
set newExportItemName to ¬
documentName & "-" & (incrementIndex as string) & "." & exportFileExtension
set incrementIndex to incrementIndex + 1
end repeat
end tell
set the targetFileHFSPath to ¬
(defaultDestinationFolder as string) & newExportItemName
tell application "Numbers"
launch
open fullPath
with timeout of 1200 seconds
export front document to file targetFileHFSPath as CSV
end timeout
close
end tell
end if
end repeat
user3439894's answer works with a few change:
exists document 1 => number of documents > 0
I'm trying to get a script working which is able to batch export .mts format video files via quicktime into .mov files in 1080p. The script fails with the following error: "The action “Run AppleScript” encountered an error: “QuickTime Player got an error: Can’t make file (document "00000.MTS") into type «class fsrf».”". I assume this has something to do with using text file paths? Note I'm not experienced with Applescript and would really appreciate any help to get this simple bit of script working. Currently it's in automator as a service:
on run {inputFiles}
if inputFiles is equal to {} then
set inputFiles to (choose file with prompt "Select the file(s) to convert:" with multiple selections allowed without invisibles)
end if
open inputFiles
end run
on open droppedItems
tell application "Finder" to set inputFolder to (container of first item of droppedItems) as Unicode text
set outputFolder to (choose folder with prompt "Select output folder:" default location (inputFolder as alias)) as Unicode text
set exportPreset to (choose from list {"Movie", "iPhone", "iPod", "480p", "720p", "1080p"} with prompt "Choose QuickTime Export Preset:") as Unicode text
if exportPreset is equal to "false" then
return
end if
repeat with currentItem in droppedItems
repeat until getProcessPercentCPU("CoreMediaAuthoringSessionHelper") is equal to ""
end repeat
tell application "Finder" to set fileName to name of currentItem as Unicode text
set fileName to text 1 thru ((fileName's length) - (offset of "." in ¬
(the reverse of every character of fileName) as text)) of fileName
convertFile(currentItem, outputFolder & fileName & ".mov", exportPreset)
end repeat
end open
on convertFile(inputFile, outputFile, exportPreset)
tell application "QuickTime Player"
set thisMovie to open inputFile
open for access file thisMovie
close access file thisMovie
export thisMovie in (outputFile) using settings preset exportPreset
close thisMovie
end tell
end convertFile
on getProcessPercentCPU(processName)
do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processName & "$/ {print $1}'"
end getProcessPercentCPU
Try changing:
set outputFolder to (choose folder with prompt "Select output folder:" default location (inputFolder as alias)) as Unicode text
to:
set outputFolder to POSIX path of (choose folder with prompt "Select output folder:" default location (inputFolder as alias))
and:
convertFile(currentItem, outputFolder & fileName & ".mov", exportPreset)
to:
set outputFile to POSIX file (outputFolder & fileName & ".mov")
convertFile(currentItem, outputFile, exportPreset)
and remove the open for access/close access commands.
Sandboxed apps don't like receiving path strings to open/save commands, but accept alias/POSIX file values okay. (If it still doesn't work then it's some other issue at play, but that's always the first thing to check when you get a filesystem permissions error as you describe.)
The error occurs because thisMovie is a document reference of QuickTime Player and this class cannot be converted / coerced to a file system reference («class fsrf»).
That's what the error message says
Can’t make file (document "00000.MTS") into type «class fsrf»
The Standard Additions command open for access does not support QuickTime Player documents anyway. What is the purpose of the open / close lines?
Note:
as Unicode text as coercion to string is outdated since macOS 10.5 Leopard. It's only used with read and write commands to handle UTF16 encoded text. A coercion to standard AppleScript text is simply written as text. In case of name of currentItem it's redundant anyway because the class of name is always text.
I receive a lot of customer vcards to a specific email address. I want to automatically add the vcards to my contacts through the Mail rules and an AppleScript.
I searched a lot and found something. I modified it a bit. And the opening and adding process works fine. But only when I choose a file. I can't get the file into a variable from the mail message. I tried it but it won't work.
Here is my code so far:
tell application "Mail"
set att to attachment
end tell
set thefile to att
tell application "Contacts"
activate
open thefile
end tell
tell application "System Events" to keystroke return
If I delete line 1, 2 and 3 and write in line 4 "set thefile to choose file" then it will work - if I choose a file.
But the first three lines I tried something out, but without any success.
So my question is, how can I get the file from the message?
Thank you
Yours sincerely,
Chris
Solution:
set Dest to ((path to desktop folder) as string)
tell application "Finder" to make new folder in Dest with properties {name:"TempFiles"} -- create TempFiles folder
Set Dest to Dest & "TempFiles:"
tell application "Mail"
activate -- not sure is mandatory, but I prefer to see selected mails !!
set ListMessage to selection -- get all selected messages
repeat with aMessage in ListMessage -- loop through each message selected
set AList to every mail attachment of aMessage -- get all attachements
repeat with aFile in AList -- for each attachement
if (downloaded of aFile) then
set Filepath to Dest & (name of aFile)
do shell script "touch " & (quoted form of (POSIX path of Filepath)) -- required because "Save" only works with existing file !
save aFile in (Filepath as alias) as native format
end if
end repeat -- next file
end repeat -- next message
end tell
tell application "Finder" to set CardList to every file of folder Dest whose name extension is {"vcf"}
tell application "Contacts"
activate
repeat with aCard in CardList
open aCard
delay 1
tell application "System Events" to keystroke return
end repeat
end tell
delay 2
-- tell application "Finder" to delete folder Dest
The file attached from email respond to the 'save" command, but not to 'open'. Then, you must first save the attached files, and later, move them to next application (add in 'Contacts' in your case).
The attachement is a member of a list of 'mail attachement' of a message : keep in mind that there could be many files attached.
Also, you can only save the attached file if its 'downloaded' attribute is true.
Last, but not least, it seems that the "save" instruction which was working nice in Snow Leopard, does not work the same in El Capitain : the file where to save must exist before the "save"...this is why I added the "touch" command to create it first (just create the entry in the tempFiles folder).
I also add at bottom of script the open vCard with the enter key to validate in Contact. You may have to add a delay to leave sometime for your computer to process the card.
if the keys broke does not work in your case, please check System Preferences accessibility settings to allow your computer to let your script control your Mac.
I added as much comments as possible to make it clear...may be too much !
set Dest to ((path to desktop folder) as string)
tell application "Finder" to make new folder in Dest with properties {name:"TempFiles"} -- create TempFiles folder
Set Dest to Dest & "TempFiles:"
tell application "Mail"
activate -- not sure is mandatory, but I prefer to see selected mails !!
set ListMessage to selection -- get all selected messages
repeat with aMessage in ListMessage -- loop through each message selected
set AList to every mail attachment of aMessage -- get all attachements
repeat with aFile in AList -- for each attachement
if (downloaded of aFile) then
set Filepath to Dest & (name of aFile)
do shell script "touch " & (quoted form of (POSIX path of Filepath)) -- required because "Save" only works with existing file !
save aFile in (Filepath as alias) as native format
end if
end repeat -- next file
end repeat -- next message
end tell
tell application "Finder" to set CardList to every file of folder Dest whose name extension is {"vcf"}
tell application "Contacts"
activate
repeat with aCard in CardList
open aCard
tell application "System Events" to keystroke return
end repeat
end tell
-- tell application "Finder" to delete folder Dest
As you can see, I filter the content of the temp folder with only files with extension 'vcd'...just in case your selected emails contain also other type of file which Contact can't handled.
At end of the script, I delete the temp folder. however, until you test it, I set this last row as a comment only (more safe !)
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: ""
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