I've discovered a bug that I don't know how to fix. So I have an Applescript that tells the mac to highlight a certain file in an already opened Finder window connected to a shared folder on a Synology NAS server. Here's the Applescript:
set theFile to "West Office Files:_Scan Inbox:LAR:1002.pdf"
tell application "Finder"
reveal document file theFile
end tell
An error pops up saying that Finder got an error: document file "West Office Files:_Scan Inbox:LAR:1002.pdf" doesn't understand the "reveal" message. I have also tried using "select" instead of "reveal" and the problem persists. However, I just discovered it doesn't throw an error the first time the script is ran. But if I run it again to select a different file (but in same folder location) it then throws the error.
Then strange things begin to happen in Finder. Can't click on a finder window showing files, can't close any windows. Sometimes Finder will start popping up what I call "shadow boxes" which is the drop shadow of a window, but the window is invisible. Finder won't correct itself and the only fix I've found is to quickly log out the user to the Mac login screen and log back in. If I don't Finder will get worse and ultimately requires a force restart.
This is ONLY happening with MacOS Sierra (any sub version) and when connected to a Synology NAS server (any version DSM 6+) via SMB. AFP works fine, pre-Sierra Mac OSes work fine, and even when connected to an old Mac OS (Lion) Server via SMB or AFP there is no problem.
Anyone experiencing this? Any ideas for a solution?
Untested (im on mobile) but you need to transform osx path to posix file:
set theFile to POSIX file (POSIX path of "West Office Files:_Scan Inbox:LAR:1002.pdf")
tell application "Finder" to reveal theFile
With help from Pat_Morita I found a solution to the problem. Am posting it here to help others who use Synology NAS servers and Macs.
set theFile to POSIX file (POSIX path of "/Volumes/West Office Files/_Scan Inbox/LAR/1002.pdf") as alias
tell application "Finder"
set selection to {}
select theFile
end tell
Where the error was occurring was specifically when the AppleScript would tell the Finder to highlight a file and there was a file already highlighted. In MacOS Sierra it would throw an error in the Apple Event right when it moves away from the currently selected file. So adding in the "set selection to {}" has Finder deselect what is highlighted before selecting the next file. I also went with using POSIX for the file path by recommendation from this forum.
UPDATE ---
False alarm on finding a fix, well... sorta.
The error is still thrown and seems to only be happening when a Finder window is in Cover Flow view. All the other views work fine. Also, I discovered the bug is happening when MacOS Sierra is connected via AFP as well, not just SMB. I changed the AppleScript a bit:
set theFile to "West Office Files:_Scan Inbox:LAR:1002.pdf" as alias
tell application "Finder"
set selection to {}
select file theFile
end tell
Learned that Finder doesn't really care if the file path is set up via POSIX or not. Also added "file" to the select line.
Related
What I ultimately want to do is have a .app that I can open DMG files with. When the DMG file is opened the app, it opens the DMG in a new Finder tab and then shows the Toolbar in Finder after half a second. AppleScript for the later would look like this:
delay 0.5
tell application "Finder"
tell the front window to set toolbar visible to true
end tell
I tried looking up how to open a DMG file in a new Finder Tab with AppleScript but didn't find a solution. If I end up not finding one, then I'll see if I can do so with a Terminal command and then do it in AS with do shell script. For the file input, I tested this code based off what I found here but it doesn't work, it just creates a copy of the DMG:
on run {input, parameters}
if input is {} then
set inputFile1 to ¬
quoted form of POSIX path of ¬
(choose file with prompt "Please select a file to process:")
else
set inputFile1 to quoted form of ¬
(POSIX path of first item of input)
end if
display dialog inputFile1
end run
I did a bunch of googling as well for how to do so but I found nothing. What I can do to end up with an AppleScript that does everything I described?
I would open the dmg with hdiutil which seems to open the image into a standard window:
set runDMG to quoted form of POSIX path of (choose file)
do shell script "/usr/bin/hdiutil attach " & runDMG
Basically, you get the posix path of your dmg file and feed it to hdiutil attach. The disk image will open and —for me at least— consistently opens in a default finder window (i.e. same size as other windows, list view, sidebar visible, toolbar visible).
I should add that typically when I double-click on a dmg, it opens in a 'drag to applications folder' window which is icon view and shows only the app and the applications folder.
Separately, this code seems to work with bringing the disk image's folder to the front window.
tell application "Finder"
parent of startup disk
--> computer container of application "Finder"
open computer container
set target of window 1 to (last item of window 1 as alias)
--> disk "Key Codes" of application "Finder"
end tell
There are (at least on my Sierra mac), three items in the 'computer' folder: 'Network', 'Remote Disc', the startup volume. When you open a dmg, the virtual appears here as a 'removable'. I've found that when you get a list of folders in the computer container, the removable is the last item in the list. I don't know if that ever changes, and I haven't played around with multiple disk images simultaneously but it provides a starting point for specifying the desired window if for some reason, it doesn't open automatically.
I am trying to see if i can change the text of some slides inside a keynote presentation that has already been created but i am having problems just opening it to begin with.
here is my current simple applescript:
set thisFile to "Users/myUserName/Desktop/KeynoteAppleScript.key"
tell application "Keynote"
activate
open thisFile
show slide 1
end tell
I get an error stating that the file I am trying to open :
"Users/myUserName/Desktop/KeynoteAppleScript.key" couldnt be opened
because there is no such file".
Thats obviously not correct, it is there and the i have double checked the name of the file to verify.
I am using Keynote 6.5.2
Script Editor Version 2.7, AppleScript 2.4
What am I doing wrong here?
Try adding a leading fwd slash to the path
e.g,
set thisFile to "/Users/myUserName/Desktop/KeynoteAppleScript.key"
I needed to do this to get it opening on 10.8, Keynote 5.3
ALSO..
When in doubt with a file path, try dragging the file from the desktop to the script window and it will insert a correct path.
I'd like to write an AppleScript program to do the following (Automator would be fine too):
I want to open the current active TextMate file (possibly there are several tabs open and other windows) with the application Transmit 2. (This will upload the file over FTP using Transmit's DockSend feature.)
Here I've used a specific application (TextMate) but ideally I'd like it to work for any file currently active in any application.
Ultimately I will assign a keyboard shortcut to run it.
Here's what I have so far:
tell application (path to frontmost application as text)
set p to path of document 1
end tell
tell application "Finder"
open POSIX file p using "Transmit 2"
end tell
I've tried many variants of this and nothing works.
EDIT:
I have found this page: http://wiki.macromates.com/Main/Howtos and someone has made exactly the script I'm looking for:
tell application "Transmit" to open POSIX file "$TM_FILEPATH"
This is for Transmit [not 2] and I think for TextMate pre v2. I get the error (when using Transmit 2):
Transmit 2 got an error: AppleEvent handler failed.
One of the updates to v2 has broken it (not sure which one).
There appear to be two steps to your problem. One, get the path to the document (or some other reference that allows you to later open the document), and, two, open the document in the desired application.
If the AppleScript is saved as an application, the frontmost application is the AppleScript you’re running, and so that path will be the path to the AppleScript application. In that case, I’m not aware of how to get the second-frontmost application.
However, if the application supports the scripts folder (go into AppleScript Editor‘s preferences, and enable “Show Script menu in menu bar”), you can save the script as a “Script“ in the User Scripts folder, and when run from the scripts menu the frontmost application will be the application you’re currently in.
You may want to display the p variable when testing to ensure that you are getting the correct path and not the path to the AppleScript.
As far as opening the document in another application (such as Transmit), the best way to do this is to talk to the application directly if it supports it:
tell application (path to frontmost application as text)
set p to path of document 1
end tell
--for testing: verify that the path is for the correct document
display dialog p
tell application "Transmit 2"
open p
end tell
I don’t have Transmit, but I’ve verified that this works if I replace “Transmit 2” with Textastic or Smultron 6.
If you do need to use the Finder to open the document, the Finder seems to prefer its paths as strings, and also seems to prefer a full path to the application. Something like this should work:
tell application (path to frontmost application as text)
set p to path of document 1
end tell
--for testing: verify that the path is for the correct document
--display dialog p
set transmitPath to path to application "Transmit 2"
set p to POSIX file p as string
tell application "Finder"
open file p using transmitPath
end tell
Again, I’ve tested this using Textastic and Smultron as the applications.
The most common solution for the problem you are trying to solve is to run an app that makes your Web server appear to be a mounted Mac disk. Transmit 4 has that feature, which Panic calls “Transmit Disk.” But there are a few other apps also — Transmit was not the first.
Your Mac apps (and AppleScripts) just see a typical Mac disk that they can save files to and read files from (the most basic of basic AppleScript tasks) and yet Transmit Disk (or similar app) is transparently mirroring any changes to that Mac disk to your Web server in the background. This makes all the network and FTP stuff totally go away and leaves you writing very simple scripts that do very powerful things to your Web server. You Save HTML documents on there, you Export image and movie files onto there as easily as you would Save them on your Desktop, and yet they are immediately published to your Web server. Even the only barely scriptable apps can Save their files onto a particular disk.
For example, if I have an HTML document open in BBEdit and I want to save a copy of that document to my Web server, it only takes a few lines of code, which would likely be similar in any AppleScript-able text editor (this script would also work verbatim in the free TextWrangler):
tell application "BBEdit"
set theHTMLSource to the contents of text window 1
make new document with properties {text:theHTMLSource}
save document 1 to file "Transmit Disk:index.html"
close document 1 saving no
end tell
Notice that the AppleScript above not only doesn’t have to know anything about SFTP or have any login credentials, it doesn’t even have to figure out the file path of my current document because it just takes the content right out of the current window. There are no POSIX pathnames, no shell scripts, no monkey business at all. And because this task and code is so simple, you could conceivably rewrite this script 20 times for 20 different apps that you may use, so that they can all Save a copy of their current document onto your Transmit Disk, and thus publish that document to your Web server.
And if I have a folder of images that goes along with that HTML document, I can ask Finder to duplicate that folder onto my Transmit Disk to publish it. With just one line of code:
tell application "Finder"
duplicate folder "images" of (the path to the desktop folder as alias) to the disk "Transmit Disk" replacing no
end tell
… but those images could also be exported out of Photoshop or any app, right onto the Transmit Disk, via AppleScript.
In short, the thing that all of your Mac apps have in common is they can all Save files to a Mac disk. They can’t necessarily all give you the pathnames to the documents they have open, or open those files with Transmit. And Mac apps and AppleScript were designed primarily to work with files Saved or Opened to/from local disks. So you gain a lot if you use something like Transmit Disk to make your Web server basically part of the AppleScript party, by making it appear to be just a plain old Mac disk.
I am trying to incorporate my applescript into xcode. the script works normally with applescript but not ran in xcode. I am trying to open the file for reading. here is the code
set Location to "US"
set DriverFile to "/Volumes/MacPrintDrivers/" & Location & "DriverInstall.txt"
set DriverInstallFile to POSIX file DriverFile
open for access DriverInstallFile
i have confirmed the file exists and i can display the contents. i can't seem to read this way in Xcode. the error i get is
«script» doesn’t understand the «event rdwropen» message. (error -1708)
ASOC (AppleScript Objective-C) has some problems with scripting addition commands (such as open for access). Sometimes you can work around this by saying tell current application to, e.g. tell current application to open for access. But for further details I suggest you get Shane Stanley's book: http://www.macosxautomation.com/applescript/apps/ He has explored this in great depth.
I am having significant issues creating a very simple AppleScript to export attachments from an Evernote note. I have used the following code several times and it seems to work, but in actuality it fails silently, even though the event log shows a correct response.
I have heard that Evernote's recent versions have significant AppleScript support issues. If someone out there is an Evernote and AppleScript "expert", I would also greatly appreciate a review of my script to make certain I'm not forgetting anything.
I am using Mac OS X 10.8.2 and the Mac App Store version of Evernote 5.0.5 (400805).
Code:
set ExportPath to "LegatusHD1:Users:me:Downloads:Test"
set theAttachments to {}
set selectedItems to {}
tell application "Evernote"
set selectedItems to selection
repeat with selectedItem in selectedItems
set theAttachments to (attachments of selectedItem)
repeat with theAttachment in theAttachments
set theFilename to (ExportPath & ":" & theAttachment's filename)
write theAttachment to theFilename
set theFilename to ""
end repeat
end repeat
end tell
Apple has introduced sandboxing for apps from the AppStore recently. So depending on your OSX version, this might be the reason why it fails silently - since the Evernote app is not able to access the file system outside its own folder structure. You can try setting the export path to be something within the Evernote folder.
This question is old but I just tripped over it looking for an answer which I can now provide:
FYI I am running: OSX 10.9.1 (Mavericks) and Evernote 5.4.4 installed via App Store.
I can confirm that Mustafa was right, using the App Store version of the app it is only possible to write to files within the sandbox (/Users/{username}/Library/Containers/com.evernote.Evernote/)
Short answer
Either install the app from the Evernote website or if you want to use the one in the App Store:
Using the above script change the export path to something inside the sandbox e.g.:
set ExportPath to "Users:{username}:Library:Containers:com.evernote.Evernote:Data:attachment_export"
Remember to make the attachment_export folder first, and replace the {username} with your username
A note on debugging
Unfortunately applescript editor doesn't show any errors when you try to write outside the sandbox. You can however view the error in the Console application (found in Applications -> Utilities). In Console enter 'sandbox' in the search box (top right hand corner) and at the bottom of the window you will see something like this:
1/29/14 10:22:29.426 sandboxd[310]: ([4433]) Evernote(4433) deny file-write-create /Users/{username}/Desktop/Evernote Snapshot 20140128 134741.jpg