Script-fu unable to scale image after duplicating it - gimp

The following is part of a larger Script-fu script that I am trying write.
I have run into a problem trying to duplicate .xcf file that is open and then scale it to some user specified dimension.
The following is what I though would work:
(define (my-duplicate-and-scale inImage inDrawable inWidth inHeight)
(let* ((theDuplicateImage (gimp-image-duplicate inImage)))
(gimp-image-scale theDuplicateImage inWidth inHeight)
)
)
(script-fu-register
"my-duplicate-and-scale" ;func name
"Duplicate and Scale ..." ;menu label
"" ;description
"" ;author
"" ;copyright notice
"" ;date created
"*" ;image type that the script works on
SF-IMAGE "Image" 0
SF-DRAWABLE "Drawable" 0
SF-VALUE "Width" "512"
SF-VALUE "Height" "512"
)
(script-fu-menu-register "my-duplicate-and-scale" "<Image>/File/My")
When I execute the function I receive the following error:
Error while executing my-duplicate-and-scale:
Error: ( : 2) Invalid type for argument 1 to gimp-image-scale
According to the Procedure Browser gimp-image-duplicate returns an IMAGE and the first parameter to gimp-image-scale is an IMAGE.

Try this code:
Replace:
(let* ((theDuplicateImage (gimp-image-duplicate inImage)))
with:
(let* ((theDuplicateImage (car (gimp-image-duplicate inImage))))

Related

What's a way to retrive the "path" portion of dxlHere() and go up two dirs?

We are regularly setting up new DOORS installations on standalone networks, and each of these networks use slightly different drive mappings and installation directories. We have a set of DXL scripts that we copy over to each network that uses DOORS, but these DXL scripts reference some Microsoft Word templates that are used as the basis for custom-developed module export scripts.
We no longer have a DXL expert in-house, and I'm trying to make the scripts more portable so that they no
longer contain hard-coded file paths. Because we copy all of the templates and DXL files in a pre-defined directory structure, I can use the dxlHere() function to figure out the execution path of the DXL script, which would print something like this:
<C:\path\to\include\file\includeFile.inc:123>
<C:\path\to\include\file\includeFile.inc:321>
<Line:2>
<Line:5>
<Line:8>
What I'd like to do is extract everything before file\includeFile.inc:123>, excluding the starting <. Then I want to append templates\template.dotx.
For example, the final result would be:
C:\path\to\inclue\template.dotx
Are there any built-in DXL functions to handle string manipulation like this? Is regex the way to go? If so, what regexp would be appropriate to handle this?
Thanks!
I got this... kind of working.
dxlHere is something I don't work with much, but this seems to work- as long as it's saved to a an actual dxl or inc file (i.e. not just run from the editor)
string s = dxlHere()
string s2 = null
string s3 = null
Regexp r = regexp2 ( "\\..*:.*> $" )
Regexp r2 = regexp2 ( "/" )
if ( r s ) {
s2 = s[ 1 : ( ( start ( 0 ) ) - 1 ) ]
s3 = s[ 1 : ( ( start ( 0 ) ) - 1 ) ]
int x = 0
while ( r2 s2 ) {
x++
s2 = s2[ ( ( start ( 0 ) ) + 1 ) : ]
}
int z = 0
for ( y = 0; y <= length( s3 ); y++ ){
if ( s3[y] == '/' ) {
z++
if ( z == ( x - 2 ) ) {
s = s3[ 0 : y ]
break
}
}
}
}
print s
So we're doing a single regexp to check if we have a valid 'location', then running through it to find ever '/' character, then leaving off the last 2 of them.
Hope this helps!

How do I write busted unit tests for functions that return ok,err?

I have a Lua function that returns false followed by error message and want to test its behavior using the busted testing framework. Right now I am doing it a bit like this:
function safe_divide(a, b)
if b > 0 then -- buggy! should be b ~= 0
return a / b
else
return false, "division by zero"
end
end
describe("safe_divide", function()
it("can divide by positive numbers", function()
local ok, err = safe_divide(10.0, 5.0)
assert.truthy(ok)
assert.are.same(2.0, ok)
end)
it("errors when dividing by zero", function()
local ok, err = safe_divide(10.0, 0.0)
assert.not_truthy(ok)
assert.are.same("division by zero", err)
end)
it("can divide by negative numbers", function()
local ok, err = safe_divide(-10.0, -5.0)
assert.truthy(ok)
assert.are.same(2.0, ok)
end)
end)
There are two things that I don't like about my current approach:
Each test is 3 lines instead of a single clean line
When the 3rd test fails, busted just says that false is not a truthy value as expected, and never mentions the "divide by zero" error message.
Is there a way I can improve my test file to avoid these problems?
I think what I want to do is kind of similar to the has_error assertion in busted but that seems to be only for functions that actually raise exceptions, not for functions that return false followed by error message.
Busted makes use of luassert, that can be extended with your own assertions.
For instance, the following code uses a user-defined assertion answers that takes as first parameter a table of the expected result, and the effective function results as remaining parameters.
local assert = require "luassert"
local function safe_divide(a, b)
if b > 0 then -- buggy! should be b ~= 0
return a / b
else
return false, "division by zero"
end
end
local function answers(state, arguments)
local expected = arguments[1]
assert(type(expected) == "table")
for i = 2, #arguments do
if arguments[i] ~= expected[i-1] then
state.failure_message = "unexpected result " .. tostring (i-1) .. ": " .. tostring (arguments [i])
return false
end
end
return true
end
assert:register("assertion", "answers", answers)
describe("safe_divide", function()
it("can divide by positive numbers", function()
assert.answers({ 2.0 }, safe_divide(10.0, 5.0))
end)
it("errors when dividing by zero", function()
assert.answers({ false, "division by zero" }, safe_divide(10.0, 0.0))
end)
it("can divide by negative numbers", function()
assert.answers({ 2.0 }, safe_divide(-10.0, -5.0))
end)
end)
This code lacks correct formatting for the assertion message. You can look at the luaassert documentation, or at the predefined assertions. It contains a say module for translations of messages.

GLib-GIO-CRITICAL error While invoking a method on dbus interface

I am using lgi dynamic Lua binding to GObject based libraries to communicate between applications through dbus.Below is some portion of output of the dbus-send command for the method_call of my application.
method return sender=:1.2 -> dest=:1.19 reply_serial=2
array [
dict entry(
string "XYZ"
variant int32 1
)
dict entry(
string "ABC"
variant int32 0
)
dict entry(
string "EFG"
variant string "str"
)
dict entry(
string "HIJ"
variant string "8c011401-4836-4889-8fa5-32ddb894a97a"
)
dict entry(
string "KLM"
variant string "dummy"
)
Now I want to set the "DEF" value in the above output which has signature as 'a{sv}'. Below is the code I wrote to do the same:
local lgi=require 'lgi'
local Gio=lgi.require 'Gio'
local GLib=lgi.GLib
local db=require 'debugger'
local factory=function()
return Gio.bus_get_sync(Gio.BusType.SYSTEM)
end
local bus=factory()
local j=GLib.Variant.new('s','value')
local k2=GLib.Variant('a{sv}',{DEF=j})
str,err=bus:call_sync('org.destination','/my/application/obj_path','org.destination.path','Method_name',k2,nil,Gio.DBusConnectionFlags.NONE,-1)
if err then
print(err)
else
print(str)
end
When I run the above script I am facing the following error:
(process:19120): GLib-GIO-CRITICAL **: g_dbus_connection_call_sync_internal: assertion '(parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE)' failed.
How to set the above value ? Where am I doing wrong ? Can someone help me ?
Thanks in advance.
The error tells you pretty clearly what the problem is. The "parameter" argument to call_sync (g_dbus_connection_call_sync in the C API) needs to be a tuple. You have provided a dictionary. Change k2 so that it is a tuple variant.

Search a section of text, which occurs regularly in tcl

I have a file called log_file with following text:
....some text....
line wire (1)
mode : 2pair , annex : a
coding : abcd
rate : 1024
status : up
....some text....
line wire (2)
mode : 4pair , annex : b
coding : xyz
rate : 1024
status : down
....some text....
The values may differ but the attributes are always the same. Is there a way to find each line wire and display their attributes? The number of line wires also may differ.
EDIT: File doesn't have any blank lines. There are more attributes but only these are needed. Can I get like the first "n" lines, instead of searching for every line? i.e if there is line wire (1), copy that line plus the next 4 lines.
And I am copying the searched lines to a output file $fout, which I have used earlier in the script with the same $line.
Given your sample:
set fh [open log_file r]
while {[gets $fh line] != -1} {
switch -glob -- $line {
{line wire*} {puts $line}
{mode : *} -
{coding : *} -
{rate : *} -
{status : *} {puts " $line"}
}
}
close $fh
outputs
line wire (1)
mode : 2pair , annex : a
coding : abcd
rate : 1024
status : up
line wire (2)
mode : 4pair , annex : b
coding : xyz
rate : 1024
status : down
Edit: print the next "n" lines following the "line wire" line to a file
set in [open log_file r]
set out [open log_file_filtered w]
set n 4
while {[gets $in line] != -1} {
if {[string match {line wire*} $line]} {
puts $line
for {set i 1} {$i <= $n} {incr i} {
if {[gets $in line] != -1} {
puts $out " $line"
}
}
}
}
close $fh

Lua Script Error

Situation:
I want to save the record of a data which is a value of the sensor in the certain file.
Code is..
--Header file
require("TIMER")
require("IPBOX")
require("ANALOG_IN")
require("LOG")
function OnExit()
print("Exit code...do something")
end
function main()
timer = "Timer"
local analogsensor_1 = "AIR_1"
local analogsensor_2 = "AIR_2"
local timestr = os.data("%Y-%m-%d %H:%M:%S")
-- open the file for writing binary data
local filehandle = io.open("collection_of_data.txt", "a")
while true do
valueOfSensor_1 = ANALOG_IN.readAnalogIn(analogsensor_1);
valueOfSensor_2 = ANALOG_IN.readAnalogIn(analogsensor_2);
if (valueOfSensor_1 > 0 and valueOfSensor_2 > 0) then
-- save values of sensors
filehandle:write(timestr, " -The Value of the Sensors: ", tostring(valueOfSensor_1), tostring(valueOfSensor_2)"\n");
-- save values using rolling log appender:
LOG.log_event( ScenarioLoggerDevicenameInDeviceList, " -The Value of the Sensors: ", tostring(valueOfSensor_1), tostring(valueOfSensor_2), "any other string you wish to add", "etc", "etc")
LOG.log_event( ScenarioLoggerDevicenameInDeviceList, " -The Value of the Sensors: ", tostring(valueOfSensor_1))
print("Hello3"..valueOfSensor_1)
end
TIMER.sleep(timer,500)
end
-- close the file
filehandle:close()
end
print("start main")
main()
In this line:
filehandle:write(timestr, " -The Value of the Sensors: ", tostring(valueOfSensor_1), tostring(valueOfSensor_2)"\n");
I get an error:
"attemp to index global 'filehandle' (a nil value)"
How can I fix it?
io.open returns nil if it cannot open the file. This can be due to "file not found", "permissions denied" and maybe other reasons. To figure out the problem, io.open has a second return value, which lets you inspect the error (in fact, it even returns a third value, which is an error-code integer - but its meaning is system dependent).
Change:
local filehandle = io.open("collection_of_data.txt", "a")
to
local filehandle, message = io.open("collection_of_data.txt", "a")
if not filehandle then
print(message)
end
You can also use the following Lua idiom:
local filehandle = assert(io.open("collection_of_data.txt", "a"))
This will do the same. If the first argument to assert is nil, then the second argument (the second return value of io.open will be printed. If the first argument is not nil, it will simply be returned.

Resources