Why does Applescript run in Script Editor, but error when saved as Application? - osx-snow-leopard

My applescript needs to detect its own filename, and the following runs fine on Snow Leopard (10.6)
set my_name to name of me as string
display dialog "Name: " & my_name
It displays "Name: AppleScript Editor" when I run it from AppleScript Editor, and it displays "Name: NewTest" when I save it as an application called NewTest.
When I run it on a Leopare (10.5) machine, it complains "Can't make name of <> into type string." When I remove the "as string" portion, it runs under Script Editor, returning "Name: Script Editor", but when saved as an application, it errors and says, "Can't get name."
What is different about running in script editor and saving as application under 10.5?

Here's another thought although I haven't checked. One thing that can cause problems is the command "get". In general when you run a command like "name of me" the command get is implied so you're really running "get name of me". The problem is that the implied "get" is not always the case. So sometimes you have to explicitly say "get". Whenever I have a problem like yours the first thing I try is to add "get" to the command... it's become habit because you just never know. Note that you can always use the word get and never have that issue. As such, try changing your command to "set my_name to (get name of me)". I'd be interested to know if that fixes your 10.5 problem. Also note that a name is already a string so there's no need to coerce the result to a string.
EDIT:
I looked through some of my older scripts. I used the following code to get the name. In my notes I have these comments...
-- this will get the name of the application or script without any file extension
-- it is done using the path because when a script is run from the script menu, and you write set myName to name of me, then the result is "applescript runner" instead of the actual name
-- also it assures that you're getting the name as it appears in the Finder because sometimes the system events process name is different than the Finder name
on getMyName()
set myPath to path to me as text
if myPath ends with ":" then
set n to -2
else
set n to -1
end if
set AppleScript's text item delimiters to ":"
set myName to text item n of myPath
if (myName contains ".") then
set AppleScript's text item delimiters to "."
set myName to text 1 thru text item -2 of myName
end if
set AppleScript's text item delimiters to ""
return myName
end getMyName

An Applescript application isn't an "application" in the truest sense of the word. A lot of contexts change, like "get path to me" will be different when run as a script or as an application, because they are still good ol' wonky Applescript as opposed to a Carbon or Cocoa-based application. Running similar code against the Finder...
tell application "Finder"
set my_name to name as string
display dialog "Finder: " & my_name
end tell
...behaves as expected because the Finder is a Carbon/Cocoa-based application.
I don't have a real answer other than to say it sounds like there was a change made to the OS relative to the Applescript framework in 10.6 that makes the call to "me" behave more as expected.
I would recommend reading the section in the Applescript guide about the me and it keywords to gain more insight into how me works.

Related

Automatisation, Load .prg file, run it with pre-defined variable

The idea is that in one folder there are two files
test.csv
test.prg
I would like to run with .bat file (or .vbs) the file test.prg with variable "2510".
It will automatically load in Visual Fox Pro (here I do not know how to run script automatically with out physically click the exclamation mark) and visual fox pro should use variable from .bat/.vbs file as 2510.
1) Open test.prg
2) Load VFP
3) Use pre defined variable from .bat/.vbs
4) Run script (automatically)
5) close VFP
Because this is daily jobs, and I'm trying to simplify as much as possible (currently I know only how to simplify by using cmd/.bat and vbs)
If I understood you right, you want to run a prg file with changing parameters and you want to change the parameter in the calling .bat or .vbs file. If it is what you wanted to do, then you could simply have the bat file content like:
cd "c:\My Folder"
"c:\Program Files (x86)\Microsoft Visual Foxpro 9\vfp9.exe" test.prg 2510
and your prg would be run with that parameter. Keep in mind that parameters passed from command line is always of character data type.
However, there is an easier way. The way you do it, you would edit the .BAT file, save it and then doubleclick to execute. You might create a VFP executable instead, in command window (assuming test.prg is in c:\My Folder'):
set default to ('c:\My Folder')
build project MyTest from 'test.prg'
build exe MyTest from 'MyTest.pjx'
and you would have MyTest.exe in that folder. Your BAT file content would then be:
cd "c:\My Folder"
MyTest 2510
It is still to cumbersome. You need to edit the .BAT file, change parameter, save and doubleclick it. Make it much simpler:
In your test.prg, instead of getting a parameter from command line, ask the parameter value and do the process! That totally removes the need for a BAT file. Then you simply create a shortcut on your desktop. Whenever you doubleclick that shortcut, it would ask for the parameter and then do processing with that parameter value and quit. The content of such a test.prg would look like:
_screen.Visible = .T.
LOCAL cInput
cInput = INPUTBOX("What is parameter value?", "Get parameter value", "2510", 5000, '', 'Cancelled')
DO case
CASE m.cInput == ''
? 'Input timed out'
CASE m.cInput == 'Cancelled'
? 'Cancelled'
CASE m.cInput == '0' Or VAL(m.cInput) != 0
Process( VAL(m.cInput) )
OTHERWISE
? 'Parameter is not numeric'
ENDCASE
QUIT
PROCEDURE Process(tnparameter)
? 'Processing with parameter =', m.tnParameter
Endproc
Also, instead of an inputbox() which returns a character value as command line parameters do, you might get the value(s) via a form with their intended types (ie: A datetimepicker on a form getting date).
It is really unclear what you are trying to do. However, from VFP, I created a simple project and program that might help you.
Start VFP. In the command window type
create project MyTest [enter]
click on the Code tab and then click new. Paste the following code snippet
LPARAMETERS DOSParm1, DOSParm2, DOSParm3, DOSParm4
MESSAGEBOX( "Parm1: " + TRANSFORM( DOSParm1 ) + CHR(13)+CHR(10);
+ "Parm2: " + TRANSFORM( DOSParm2 ) + CHR(13)+CHR(10);
+ "Parm3: " + TRANSFORM( DOSParm3 ) + CHR(13)+CHR(10);
+ "Parm4: " + TRANSFORM( DOSParm4 ) + CHR(13)+CHR(10) )
RETURN
Save the program as MyTest.prg, then click on build for the project to create an executable. Now you have a simple EXE file that accepts up to 4 parameters from the dos command or other methods (vbs). You can change the actual VFP to act on whatever variables you need, but I just have them as messagebox output display. If no parameters are provided, the default values would be logical .F. (false)
To test from a DOS prompt, you can do something like
MyTest oneParm anotherParm 3rd last
and you will get the message box displaying these 4 parameter strings.
If you skip parameters, no problem.
MyTest Only TwoParms
Again, the code can be changed to do whatever you need with your "2510" variable reference and act accordingly.

Passing an argument into a VBS script which passes into a batch file

I have a legacy application which doesn't support utilizing the default applications defined in windows which requires that I specify a specific an executable for a file format to be opened within the application. Since Microsoft no longer includes MODI with Office by default I have been looking at using launching Windows Picture Viewer for .TIF, .TIFF, & .BMP files since it is built into Windows; however Microsoft does not have a direct executable for Windows Picture Viewer which can be called forcing me to create a script which executes the command which calls for Windows Picture Viewer to execute. After research the only way I have been able to call the application to open a specific file is by creating a batch file such as below:
GIFTS.BAT
rundll32.exe C:\WINDOWS\system32\shimgvw.dll,ImageView_Fullscreen %~1
If I execute the above code such as GIFTS.BAT "C:\Example Directory\Sample File.tif" from a command prompt or from the application launches and the Sample File.tif opens without a problem; however a command prompt opens along with the file when launching from the application.
Upon which I tried to create a vbscript to hide the batch file from executing however I can't seem to pass my argument if the argument has a "space" within the argument.
GIFTS.VBS
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """C:\GIFTS.BAT"" " & WScript.Arguments.Item(0), 0
Set WshShell = Nothing
If I try to execute the VBScript from a command prompt such as GIFTS.VBS "C:\Example Directory\Sample File.tif" the application never launches and the command prompt returns no message or error. If I simplify the execute command (removing spaces to GIFTS.VBS "C:\Sample_File.tif" the application launches as well as the file Sample_File.tif is displayed and there is no command prompt displayed when the application executes the VBScript.
My question is how can I pass an argument into the VBS script that in return passes the the batch file when the argument contains spaces (which there is always going to be spaces since the argument will be a file name and path)?
There might be an easier approach to what I want to accomplish; however I am looking for a solution that Windows 7 - 8.1 can utilize with no additional software to install or manage on each workstation. The batch files works great I just need to be able to hide the command prompt that opens along with the application as my end users won't know what to do with it.
Thanks!
Sometimes, nested levels of escaping characters requires intimate knowledge of the undocumented behavior of CMD or some voodoo. Another way to attack the problem is to guarantee that you won't have any spaces in the name of the file. Windows has a concept of a short path (no spaces or special chars) for which every existing file has a unique one.
Here's a modified version of your program for which invoked subcommand doesn't need quotes around the file name.
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoFile = fso.GetFile(WScript.Arguments.Item(0))
WshShell.Run """c:\GIFTS.BAT"" " & fsoFile.ShortPath, 0
Set WshShell = Nothing
You may wish to add your own error checking. The specified file must exist in order for the GetFile() command to succeed.

Applescript or Automator to save Mandelbrot image from website progressivly

Hello I have recently created a C program for my UNI Computing course that generates a web server at localhost:2020 and sends a bmp file of the Mandelbrot set. If you dont know what that is dont worry, its the url part thats important.
The URL is formatted as follows
http://X_(x coordinate)_(y coordinate)_(Zoom Level).bmp
so
http://localhost:2020/X_-0.15_1.03_56.bmp
returns
x: -0.15
y: 1.03
zoom: 56
My goal here is to have an automated process that can take in an x,y position (in the code is fine) and repetitively load the image from the server, each time with a zoom level increased by .01 and save it to either a folder or preferably load them all into a file to be presented as a video.
Im well aware that this would be easier to do in C and just have it save to the file but my goal is to familiarise myself with applescript/automator or a similar program with tasks like this.
Its designed to be a fun learning experience for myself and I will really appreciate any help I can get thank you.
Something like this may work for part of your task. We are downloading all of the images (at each zoom level) using the unix command line utility "curl". Each image is saved with the name from the url to a folder that you choose. We put this code inside a repeat loop so we can increment the zoom level.
The script shows lots of stuff, particularly how to insert variables directly into an applescript (e.g. hard-coded) and how to get input from the user. It also shows how to run command line utilities from within an applescript (e.g. curl).
So this script should get you started. See if it helps.
-- hard-coded variables
set minZoomLevel to 0
set maxZoomLevel to 10
set zoomIncrement to 0.1
-- get user input variables
set outputFolder to choose folder with prompt "Pick the output folder for the images"
set xDialog to display dialog "Enter the X coordinate" default answer ""
set yDialog to display dialog "Enter the Y coordinate" default answer ""
set posixStyleOutputFolder to POSIX path of outputFolder
set x to text returned of xDialog
set y to text returned of yDialog
set i to minZoomLevel
repeat while i is less than or equal to maxZoomLevel
set fileName to "X_" & x & "_" & y & "_" & (i as text) & ".bmp"
set theURL to "http://localhost:2020/" & fileName
do shell script "curl " & theURL & " -o " & quoted form of (posixStyleOutputFolder & fileName)
set i to i + zoomIncrement
end repeat

.vimrc for C developers

There is a question How to set .vimrc for c programs?, but nothing especially interesting in there.
By what .vimrc options do you facilitate your C development in Linux? (e.g. for building, ctags, tabs...) Any ideas welcome, especially for "external building with make".
how about this?
https://mislav.net/2011/12/vim-revisited/
set nocompatible " choose no compatibility with legacy vi
syntax enable
set encoding=utf-8
set showcmd " display incomplete commands
filetype plugin indent on " load file type plugins + indentation
"" Whitespace
set nowrap " don't wrap lines
set tabstop=2 shiftwidth=2 " a tab is two spaces (or set this to 4)
set expandtab " use spaces, not tabs (optional)
set backspace=indent,eol,start " backspace through everything in insert mode
"" Searching
set hlsearch " highlight matches
set incsearch " incremental searching
set ignorecase " searches are case insensitive...
set smartcase " ... unless they contain at least one capital letter
https://github.com/jslim89/dotfiles
This is my repo. Inside already got a few type of vim plugins including c.vim, ctags, autocomplete, etc.
Along with options in plan9assembler's answer,
Run make from inside vim, you can just use :make but that won't automatically open the quickfix window with you're errors. To get that to happen, add a second :Make command [1]:
command! -nargs=* Make write | make! <args> | cwindow
Another thing I have is a recursive search for my ctags file. The following will use the tags file in the current directory, then search one directory above recursively till it finds a tag file [2]:
set tags=./tags;

Applescript testing for file existence

OK, I thought this would be a simple one, but apparently I'm missing something obvious. My code is as follows:
set fileTarget to ((path to desktop folder) & "file$") as string
if file fileTarget exists then
display dialog "it exists"
else
display dialog "it does not exist"
end if
Easy right? Unfortunately, when I run the script it returns the error
Can’t get file "OS X:Users:user:Desktop:files$".
It doesn't matter if the file exists or not, this is the same error I get. I've tried a dozen different things but it still stumps me.
I use this subroutine to see if a file exists or not:
on FileExists(theFile) -- (String) as Boolean
tell application "System Events"
if exists file theFile then
return true
else
return false
end if
end tell
end FileExists
Add salt to taste.
It is easy except "exists" is a Finder or System Events command. It's not a straight applescript command. As such you must wrap it in a tell application code block. FYI: here's another way that doesn't require an application. It works because when you coerce a path to an "alias" it must exist otherwise you get an error. So you could do the following.
set fileTarget to (path to desktop folder as text) & "file$"
try
fileTarget as alias
display dialog "it exists"
on error
display dialog "it does not exist"
end try
NOTE: you have an error in your code. You're using the & operator to add strings but you're doing it wrong although you're getting the right answer by luck. When you use the & operator, each object on either side of the operator must be a string. "path to desktop folder" is not a string so we first must make that a string and then add the string "file$" to it. So do it like this...
set fileTarget to (path to desktop folder as text) & "file$"
to avoid original error
Can’t get file "OS X:Users:user:Desktop:files$".
simply add to script tell block
tell application "Finder"
set fileTarget to ((path to desktop folder) & "file$") as string
if file fileTarget exists then
display dialog "it exists"
else
display dialog "it does not exist"
end if
end tell

Resources