Difficulty using Applescript in a Service
CompletedVersion 1.0.0 (69)
on 2015 MacBookPro, running OSX 10.12.6 (16G2136)
---
Trying to write a service as I have in past for nvAlt, to drop an image from the Clipboard into the current note as an image link.
I am using the interface explained in https://nvultra.com/help/advanced-features#applescriptsupport
My Applescript runs just perfectly in the Automator: putting the image in a file in the current directory, and inserting an md image link into the current note. But it will not run correctly when invoked as a service from within nvUltra. It just hangs when it tries to get the file of _doc from nvUltra.
It could be that I am missing something obvious; I am not a great AppleScript hacker.
Thanks for advice.
-Jeremy Elliott
```
#####
# Util service for nvAlt,
# Take image in clipboard,
# and insert it into current note.
#
# details: saves the image to a file (named w/ datetime by default),
# in an ./_images directory inside the current nvAlt working folder.
# then inserts an md image link to that file in the current nvAlt note.
# -jelliott (databigot) 2018
# def fetch nvAlt working folder, expand any ~, return POSIX path
to fetch_nvAlt_working_folder()
### Get the current nvAlt working folder:
#set nvPlistFile to "/Users/me/Library/Preferences/net.elasticthreads.nv.plist"
set nvPlistFile to (path to preferences as string without folder creation) & "net.elasticthreads.nv.plist"
set curDirItemName to "NSNavLastRootDirectory"
tell application "System Events"
tell property list file nvPlistFile
set curDir to (value of property list item curDirItemName)
end tell
end tell
#### expand out the ~ home dir.
if character 1 of curDir contains "~" then set curDir to POSIX path of (path to home folder) & text (2 + (offset of "~" in curDir)) thru -1 of curDir
return (POSIX path of curDir)
end fetch_nvAlt_working_folder
to fetch_nvUltra_working_folder()
# as per https://nvultra.com/help/advanced-features#applescriptsupport
tell application id "com.multimarkdown.nvUltra"
-- target the foreground window
set _doc to document 1
-- Get the URL of the parent folder for the window
# !! it hangs on this next line:
set parent_folder to POSIX path of (get file of _doc)
-- Get the URL of the currently selected note
# set current_note to (get note of _doc)
end tell (* Your script goes here *)
return parent_folder
end fetch_nvUltra_working_folder
# def make folder at fullPath (posix string)
to folder_makeexist(fullPath)
set newPath to (do shell script "dirname \"" & fullPath & "\"")
set newFile to (do shell script "basename \"" & fullPath & "\"")
tell application "Finder"
if not (exists (fullPath)) then
make new folder at (newPath) with properties {name:newFile}
display dialog "Folder created: " & fullPath
end if
end tell
end folder_makeexist
to gen_tempfilename()
# def make tempfilename from currentdate, ext
### Default to a filename based on YYMMDD_TIME.jpg:
set {year:yr, month:mn, day:dy} to (current date)
set mytime to time of (current date)
set fileName to (yr as string) & mn & (dy as string) & "_" & (mytime as string) & ".jpg"
return fileName
# eodef
end gen_tempfilename
to file_makeFromContent(fullPath, content)
# def make file at fullPath with content
#todo: check overwrite?
set newPath to (do shell script "dirname \"" & fullPath & "\"")
set newFile to (do shell script "basename \"" & fullPath & "\"")
tell application "System Events" to write content to (make new file at folder (newPath) with properties {name:newFile})
# eodef
end file_makeFromContent
on run {input, parameters}
if ((clipboard info) as string) does not contain "JPEG picture" then
display dialog "the clipboard does not contain a JPEG picture"
else
set myImage to (the clipboard as JPEG picture)
# set thePath to fetch_nvAlt_working_folder()
set thePath to fetch_nvUltra_working_folder()
### Create an Image subfolder under the main nvALT folder if not exists.
#(fixed from previous, because High Sierra prob:doesn't work if folder doesn't exist)
set imageFolder to POSIX file (thePath & "/" & "_images")
folder_makeexist(imageFolder)
set fileName to gen_tempfilename()
### Allow user to set folder and name for image file:
set fullPath to POSIX path of (choose file name with prompt "Name image file as: " default name fileName default location (imageFolder)) as string
file_makeFromContent(fullPath, myImage)
display dialog "Created Image File: '" & fullPath & "'"
### todo: maybe append the image link into a magic file that lists all images?, _Images.md?? just an idea.
### insert the image-link text back into the nvAlt document
set outMessage to ""
return "
" & outMessage & "
"
end if
end run #run
```
-
Tried to do some quick testing with this, but I'm having trouble getting your script to run (Logical operator NOT used with other than 1 term , but it doesn't tell me which line and all of the "not" statements appear correct...). Either way, I've had some issues with Services hanging any time I call them from within the editor, so I need to track down a general bug that very likely is the same one you're experiencing.
If you have the above script saved as a service and can share a link to a zipped version of it, that might help me. My services aren't consistently causing the issue, and it's way harder to track down intermittent bugs.
0 -
i've pared it down to the basics in https://multimarkdown.zendesk.com/hc/en-us/requests/161 , and it doesn't work with just the simple call.
0 -
Recent versions have had some changes made to AppleScript suppport. Are you still having trouble?
0
Please sign in to leave a comment.
Comments
3 comments