Getting the names of the libraries in Bento using Applescript - applescript

I'm struggling to get going using Applescript and Bento. Initially I'd like to get the names of the libraries and I'm trying to using the following script:
tell application "Bento"
repeat with i from 1 to count of libraries
tell library i
try
set theName to name of library i
log theName
end try
end tell
end repeat
end tell
With a typical error message being:
get name of library 1 of library 1
--> error number -1728 from «class GLib» 1 of «class GLib» 1
Can anyone guide me in the right direction to get going with Applescript and Bento.

Turns out it was my error in the use of Applescript:
tell application "Bento"
repeat with i from 1 to count of libraries
set theName to name of library i
log theName
end repeat
end tell

Related

Batch Convert *.numbers to *.csv AppleScript

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

Add vcard to Contacts with Mail rules and Applescript

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 !)

How to get the name and path from applescript

I have a file, say, "123.xyz", opened in TextWrangler.
How can I use Applescript to get the "its absolute path + full name" of the currently opened file.
So that I could use this in another program or script.
I modified some code I found online, but it didn't work for me.
tell application "TextWrangler"
tell document 1
-- Get (absolute path + filename) of the current file
set FileName to «class ppth» as text
end tell
end tell
Any help is highly appreciated.
tell application "TextWrangler"
--This will return the file path of document 1 in the following format: file "Macintosh HD:Users:UserName:Desktop:document1.txt"
set filePath1 to file of document 1
--This will convert the file path of document 1 to the following format: "Users/UserName/Desktop/document1.txt"
set filePath2 to POSIX path of filePath1
end tell
This SHOULD work:
tell document 1 to set myVeryOwnPath to path as text

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.

Get only regular files from a list of aliases in AppleScript

If I have a list of aliases, how can I either remove the ones that are not regular files or create a new list with only regular files. The main question is how to determine if an alias is a regular file. I tried this, but it's hacky and it doesn't always work (like with .app files).
if (theFile as string) does not end with ":" then ...
How can I do this?
You can use the "kind" property of a file to determine what it is...
set theFile to choose file
tell application "System Events"
set theKind to kind of theFile
end tell
if theKind is not "Application" then
return "Not an application"
else
return "Is an application"
end if
It seems kind of hacky, but this seems to work well:
tell application "Finder"
set regularFiles to {}
repeat with theFile in theFiles
if the URL of theFile does not end with "/"
set end of regularFiles to theFile
end if
end repeat
end tell
I initially tried testing the path for a ":" at the end, but it breaks for bundled applications and similar files-which-are-really-folders.

Resources