What are error -10004 and error -10000 in Applescript - path

I have an Applescript that is working on my computer, but not on my colleague's. I get two errors when manipulating paths: -10004 and -10000. I have an idea on how to solve this, but first I'd like to understand those error codes.
Here is the script (I removed useless part, the full version is on github):
-- export all layers to image files
-- Settings
property exportFileExtension : "png"
property ADD_CANVAS_NUMBER : true
-- End of Settings
on file_exists(FileOrFolderToCheckString)
try
alias FileOrFolderToCheckString
return true
on error
return false
end try
end file_exists
tell application "OmniGraffle Professional 5"
set theWindow to front window
set theDocument to document of theWindow
set theFilename to name of theDocument
-- remove .graffle
-- FIRST ERROR IS HERE -10004
set theFilename to text 1 thru ((offset of "." in theFilename) - 1) of theFilename
set export_folder to (choose folder with prompt "Pick the destination folder") as string
set export_folder to export_folder & theFilename & ":"
-- create folder
if file_exists(export_folder) of me then
try
display alert "The file already exists. Do you want to replace it?" buttons {"Cancel", "Erase"} cancel button 1
on error errText number errNum
if (errNum is equal to -128) then
return
end if
end try
-- deletes the folder (necessary because some layers may have been renamed
do shell script "rm -rf " & quoted form of POSIX path of export_folder
else
-- creates the folder
do shell script "mkdir -p " & quoted form of POSIX path of export_folder
end if
set canvasCount to count of canvases of theDocument
set i to 0
repeat with canvasNumber from 1 to canvasCount
set theCanvas to canvas canvasNumber of theDocument
set canvas_name to name of theCanvas
set canvas of theWindow to theCanvas
set layerCount to count of layers of theCanvas
-- ...
set area type of current export settings to current canvas
set draws background of current export settings to false
set include border of current export settings to false
set canvas_filename to ""
-- ...
set canvas_filename to canvas_filename & canvas_name
repeat with layerNumber from 1 to layerCount
set theLayer to layer layerNumber of theCanvas
if (theLayer is prints) and (class of theLayer is not shared layer) then
set layer_name to name of theLayer as string
set filename to canvas_filename & " - " & layer_name & "." & exportFileExtension
set export_filename to export_folder & filename
-- show the layer, export, then hide the layer
if character 1 of layer_name is not "*" then
set visible of theLayer to true
-- SECOND ERROR IS HERE -1000
save theDocument in export_filename
set visible of theLayer to false
end if
end if
end repeat
end repeat
end tell
Here is the log:
tell application "OmniGraffle Professional 5"
get window 1
--> window id 5032
get document of window id 5032
--> document "MSD.graffle"
get name of document "MSD.graffle"
--> "MSD.graffle"
offset of "." in "MSD.graffle"
--> error number -10004
end tell
tell current application
offset of "." in "MSD.graffle"
--> 4
end tell
tell application "OmniGraffle Professional 5"
choose folder with prompt "Pick the destination folder"
--> alias "Macintosh HD:Users:Romain:Desktop:Temp:"
display alert "The file already exists. Do you want to replace it?" buttons {"Cancel", "Erase"} cancel button 1
--> {button returned:"Erase"}
do shell script "rm -rf '/Users/Romain/Desktop/Temp/MSD/'"
--> error number -10004
end tell
tell current application
do shell script "rm -rf '/Users/Romain/Desktop/Temp/MSD/'"
--> ""
end tell
tell application "OmniGraffle Professional 5"
...
...
save document "MSD.graffle" in "Macintosh HD:Users:Romain:Desktop:Temp:MSD:1- Navigation - 1Layout.png"
--> error number -10000
Result:
error "OmniGraffle Professional 5 got an error: AppleEvent handler failed." number -10000
Thanks!
I updated the script but I still get error -10000. Here are the modified lines:
save theDocument in file exportFilename
and
-- Create folder if does not exist, remove it otherwise
-- Shell script should not be executed inside tell application block
if file_exists(export_folder) of me then
try
display alert "The file already exists. Do you want to replace it?" buttons {"Cancel", "Erase"} cancel button 1
on error errText number errNum
if (errNum is equal to -128) then
return
end if
end try
tell me
-- Delete the folder
do shell script "rm -rf " & quoted form of POSIX path of export_folder
end tell
else
tell me
-- Create the folder
do shell script "mkdir -p " & quoted form of POSIX path of export_folder
end tell
end if

Errors -10000 - -10015 are event registry errors.
Error -10000 is not a target error per se, because it will throw an -1708 in those cases mostly. most of the time it is not a target error but an incomplete command or wrong usage of brackets. What if you use:
save theDocument in file export_filename
Error -10004 is a privilege violation error, which mean you're doing something with the file that isn't allowed. Probably you're not allowed to remove the file and do shell script command should always be used outside tell application blocks. The problem is that the target application can run as another user than the script. I'm not saying it is the error but there is a chance that it this is the problem. Otherwise you simply heve not enough privileges and you need to ask the user for administrator privileges.
do shell script "do something" with administrator privileges.

I haven't found where those error codes are documented, but they mainly deal with events that the targeted application isn't able to do. The first two errors -10004 are from using a Standard Additions command inside an application tell statement (offset and do shell script) - the application doesn't know what those commands are, passes the error up the chain to AppleScript, but AppleScript knows what they are and does it.
I don't have OmniGraffle, but the last error is telling you that the save command couldn't be performed, probably due to a problem with the destination not being a file specifier - it is just a text string, so you will probably have to coerce it into something that the command wants.

Related

What would the equivalent code be in JXA for getting the URL from a Firefox browser?

tell application "System Events" to get value of UI element 1 of combo box 1 of toolbar "Navigation" of first group of front window of application process "Firefox"
I am using the above in an AppleScript to get the URL from the Firefox browser, what would the equivalent be using JXA.
I am using JXA rather than an AppleScript because applescripts hate when you don't have a specific browser installed but still use it in the script.
Here I tested the apple-script you mentioned from the GitHub site that you would like to translate into JXA.
It contains 2 important bugs: 1) When the script is run, the front application is one, which executes the script and not the browser. This has been erroneously ignored. 2) If you do not have the Google Chrome application installed, then the script will not even compile.
The following Apple-script fixes these 2 critical bugs. I am not a JXA expert, so I leave my script as is.
property chromium_variants : {"Google Chrome", "Chromium", "Opera", "Vivaldi", "Brave Browser", "Microsoft Edge"}
property webkit_variants : {"Safari", "Webkit"}
property browsersList : chromium_variants & webkit_variants & "firefox"
tell application "System Events"
repeat 10 times
set frontApp to name of first process whose frontmost is true
if browsersList contains frontApp then exit repeat
set visible of process frontApp to false
end repeat
end tell
if (frontApp starts with "Safari") or (frontApp starts with "Webkit") then
set videoURL to run script "tell application " & frontApp & " to return URL of front document"
set videoTitle to run script "tell application " & frontApp & " to return name of front document"
else if frontApp is "Firefox" then
set videoURL to my firefoxCurrentTabURL()
set videoTitle to my firefoxCurrentTabUTitle()
else if (frontApp starts with "Google Chrome") or (frontApp starts with "Chromium") or (frontApp starts with "Opera") or (frontApp starts with "Vivaldi") or (frontApp starts with "Brave Browser") or (frontApp starts with "Microsoft Edge") then
set videoURL to run script "tell application " & frontApp & " to return URL of active tab of first window"
set videoTitle to run script "tell application " & frontApp & " to return title of active tab of first window"
else
return "You need a supported browser as your frontmost app"
end if
return {videoURL:videoURL, videoTitle:videoTitle}
on firefoxCurrentTabURL()
-- Store the current clipboard contents.
set theClipboard to (the clipboard as record)
-- Set the clipboard to a default blank value
set the clipboard to ""
-- Bring Firefox to the front, highlight the URL in the URL field and copy it.
tell application "System Events"
set frontmost of application process "firefox" to true
keystroke "lc" using {command down}
end tell
-- Read the clipboard contents until either they change from "" or a second elapses.
repeat 10 times
delay 0.2
set theURL to the clipboard
if theURL is not "" then exit repeat
end repeat
-- Restore the old clipboard contents.
set the clipboard to theClipboard
return theURL
end firefoxCurrentTabURL
on firefoxCurrentTabUTitle()
tell application "System Events" to tell process "firefox"
set frontmost to true
set the_title to name of windows's item 1
set the_title to (do shell script "echo " & quoted form of the_title & " | tr '[' ' '")
set the_title to (do shell script "echo " & quoted form of the_title & " | tr ']' ' '")
end tell
end firefoxCurrentTabUTitle

Linux expect newline

I have a custom package I want to install automatically in my docker using expect.
The first thing the package asks me to do is press Enter to continue, then it prints another 2 empty lines then it waits for an input.
My expect script :
#!/usr/bin/expect -f
set timeout -1
spawn ./install
expect "\n"
send -- "\n"
But as you can see in the image, it just runs the installer and exits.
I tried removing the expect "\n" so only send -- "\n" will execute but now even the install message doesn't appear (tried with set timeout 1000 before send and it also didn't work)
Any ideas?
P.S : This is a link to the package if anyone wants to have a go at it:
https://www.bayometric.com/downloads/digital-persona/DP_UareU_Linux223_20140429.2.zip
(the installer is inside DP-UareU-RTE-2.2.3-1.20140429_1533.tar.gz)
expect "\n" match a linefeed exactly, I think this is not what your program is sending.
To wait for a Shell prompt you can use expect "%" or expect "*" to match anything.
If you need to make sure you're dealing with the right prompt you may be able to use something like expect "*Linux Installation*".
Also don't send \n but \r for the enter key :
#!/usr/bin/expect
spawn ./install
expect "*Linux Installation*"
send "\r"
expect eof
Note that the default flag is -gl for glob pattern matching but you can also use the -re flag for regular expression matching.

dxl CreateProcess failed for system cmd instruction

I want to call run a file called csvplot.vbs (from this site) to turn a .csv file I have written using dxl (has 5 columns, each with a heading and then just numerical data) into a graph (stored as .png).
I have run the following instruction directly through cmd with success:
#echo off
cscript //nologo C:\Users\Administrator\csvplot.vbs C:\PROGRA~1\IBM\Rational\DOORS\9.6\lib\dxl\addins\Verification\Statistics\statGenTest_Top_Level.csv C:\PROGRA~1\IBM\Rational\DOORS\9.6\lib\dxl\addins\Verification\Statistics\statGenTest_Top_Level.png 800 600 1 3 1 4 1 5
pause
This produces the desired .png file.
What I want, however, is to be able to execute this through DOORS, so that whenever the script that generates the raw data is run, it also produces a graph.
What I have is this as my test case:
string echostr = "#echo off"
string commands = "cscript //nologo C:\\Users\\Administrator\\csvplot.vbs C:\\PROGRA~1\\IBM\\Rational\\DOORS\\9.6\\lib\\dxl\\addins\\Verification\\Statistics\\statGenTest_Top_Level.csv C:\\PROGRA~1\\IBM\\Rational\\DOORS\\9.6\\lib\\dxl\\addins\\Verification\\Statistics\\statGenTest_Top_Level.png 800 600 1 3 1 4 1 5"
system("cmd /c start #echo off") // doesn't recognise echo command
system("cmd /c start " commands "")
I get an error:
"Windows cannot find '#echo'. Make sure you typed the name correctly,
and then try again."
I am at a loss on how to get the script to run though cmd from dxl, and I would appreciate any help. I've only had one previous foray into system() prompts through dxl, and it was only to open a .pdf. In the meantime I will keep trying to work this out. Please let me know if I can provide any further information.
Edit: Further Information
#echo: I removed the # to see how it operates, it brings up a blank
cmd window and performs no further action. In order to even run the things in the points below, I left the # off.
I deleted "/c start" from the second system() line: this opens one command line with the usual white text at the top, and a second over the top that is completely blank.
I changed the first line as follows, and commented out the second:
system("cmd /c start echo off" "\n" commands "")
--- this got a similar result to the second dot-point, but only with one cmd window, the black (no text one)
If I don't include the "\n" marker then I get a cmd window with text of "off" commands (where commands is the defined string above).
If I only have the system("cmd /c start " commands "") line, and not the echo line, then a cmd window briefly flashes and disappears and no further results demonstrating the success of the script appear.
So my issue is this: I know this script works when run directly through command line, the problem I have is that I cannot now run it through dxl.
I have developed a solid work-around that does exactly what I need.
The issue was that the input I had dxl writing was not going through command line correctly.
Knowing that the script ran from cmd correctly and, in turn, that the script executed from a batch file correctly, and that I could run the batch file from dxl, my solution was as follows:
Define the paths in dxl using the format C:\PROGRA~1\PATHNAME\
Using the Stream write() command to write the instructions directly
to a .bat file
Then using the system() command to run the .bat file
I have included some of my code, so that maybe it might help someone attempting to do the same thing. (I'll gladly take any advice on better programming conventions.)
// functions used: genFileName(), assume if a variable is not declared here, it was declared under my globals
// genFileName() returns a string of the file name, replacing any " " with "_" so cmd doesn't cry when I run it
string basename = genFileName()
string fcsv = basename ".csv"
string csvPath = "blahblahthefilepath" fcsv
if(fileExists_(csvPath)) isFile = true
Stream fOut = append(csvPath)
// === if file does not exist, create, give column names
if( !isFile){
fOut << "Date and Time,count1,count2,count3,count4" "\n"
}
else ack ("File name exists, append stats to file?" // may not be necessary
// === print to file ===
fOut << datetime "," ctot "," ctc "," cti "," ctnc "\n"
// ===== Create Batch file to run grapher ===
string columnsToPlot = "1 3 1 4 1 5" // ==> may develop this to allow user to choose
string graphDim = "800 600" // ==> px dim, may develop for user choice
string fbat = basename ".bat"
string batPath = "blahblahthefilepath"
Stream batOut = write(batPath fbat)
batOut << "#echo off" "\n"
batOut << "title Batch file to plot statistics for " fcsv "\n"
batOut << "cscript //nologo " batPath "csvplot.vbs " batPath fcsv " " batPath basename ".png " graphDim " " columnsToPlot ""
system("cmd /c start " batPath fbat "")
// some infoBox feedback DB to tell the user that the files were created
Good luck to anyone else who is attempting something similar, and I hope this is of use to someone.
Does running the dxl script without the # in front of the echo command work?

How to execute batch file silently in windows background

i have a batch file which helps to start my rails server.when i am starting my batch file the command prompt is opening but here i need the cmd should not visible to user or it will execute at windows background.I am explaining mt .bat file code below.
c:
cd c:\\Site\swargadwara_puri
rails server
Please help me.
You could run it silently using a Vbscript file instead. The Run Method allows you running a script in invisible mode. Create a .vbs file like this one :
Option Explicit
Dim MyBatchFile
MyBatchFile = "C:\New Floder\toto 1.bat"
Call Run(MyBatchFile,1,False) 'Showing the console
Call Run(MyBatchFile,0,False) 'Hidding the console
'*********************************************************************************
Function Run(MyBatchFile,Console,bWaitOnReturn)
Dim ws,Result
Set ws = CreateObject("wscript.Shell")
'A value of 0 to hide the MS-DOS console
If Console = 0 Then
Result = ws.run(DblQuote(MyBatchFile),Console,bWaitOnReturn)
If Result = 0 Then
'MsgBox "Success"
Else
MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
End If
End If
'A value of 1 to show the MS-DOS console
If Console = 1 Then
Result = ws.run(DblQuote(MyBatchFile),Console,bWaitOnReturn)
If Result = 0 Then
'MsgBox "Success"
Else
MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
End If
End If
Run = Result
End Function
'*********************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'*********************************************************************************
The second argument in this example sets the window style. 0 means "hide the window, and 1 means "show the window"
Complete syntax of the Run method:
object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Arguments:
object: WshShell object.
strCommand: String value indicating the command line you want to run. You must include any parameters you want to pass to the executable file.
intWindowStyle: Optional. Integer value indicating the appearance of the program's window. Note that not all programs make use of this information.
bWaitOnReturn: Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).
You can minimize the batch command, for example using:
START /MIN rails server

Pre-Action run script to clear the simulator's Application Support directory

I am running automated UI tests that depend on a clean build for every run.
I'd like to add a pre-action run script that clears out anything in the Application Support directory (e.g. /Users/username/Library/Application Support/iPhone Simulator/7.1/Applications/58A5DCF7-689B-4D13-B178-A88CDE33512/Library/Application Support)
Is there an Xcode variable that makes it easy to get to that directory? Anything like $(BUILD_DIR)?
Here is what works for me:
#!/bin/bash
# `menu_click`, by Jacob Rus, September 2006
#
# Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
# Execute the specified menu item. In this case, assuming the Finder
# is the active application, arranging the frontmost folder by date.
osascript <<SCRIPT
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
application "iPhone Simulator" activate
menu_click({"iPhone Simulator", "iOS Simulator", "Reset Content and Settings…"})
tell application "System Events"
tell process "iPhone Simulator"
tell window 1
click button "Reset"
end tell
end tell
end tell
SCRIPT
Taken from: https://stackoverflow.com/a/14811280/1041311
Also note that it wonk work for the first time, since you need to allow Xcode to modify folders. At first run there will be dialog which will take you to System preferences where you need to give Xcode that permissions.

Resources